0

I am working on a simple dataset which is best analysed with t-tests, but there are at least 2 dozen t-tests, which creates a lot of lines of code and many tables! I was wondering if the there is a way to have one line of code that has all the y-variables, as my grouping variables remain the same, and then they show up in one table. Thank you for your help. Also, if there is a simple way to export the results to word or R in table format, please let me know. I'd appreciate it.

Here is a sample of my code.

t.test(XC[tissue =="Ent"], XC[tissue =="Stom"])
t.test(XN[tissue =="Ent"], XN[tissue =="Stom"])
t.test(CN[tissue =="Ent"], CN[tissue =="Stom"])
Robert
  • 5,038
  • 1
  • 25
  • 43
Rachel
  • 41
  • 5

1 Answers1

0

You can use the apply function

dta=data.frame(XC,XN,CN,tissue)
res=data.frame(apply(dta[,1:3],2,function(z)(unlist(t.test(z~dta$tissue))[1:8])),stringsAsFactors = F)
res[]=sapply(res,as.numeric)
round(res,6)

You will have something like:

# > round(res,6)
#                                      XC         XN         CN
# statistic.t                    -109.63424 -10.842582 -45.532057
# parameter.df                    101.66755 105.801883 103.753688
# p.value                           0.00000   0.000000   0.000000
# conf.int1                       -20.25112  -2.304988 -10.219741
# conf.int2                       -19.53135  -1.592335  -9.366679
# estimate.mean in group Ent       20.16557  10.044373  20.172782
# estimate.mean in group Stom      40.05680  11.993035  29.965992
# null.value.difference in means    0.00000   0.000000   0.000000
Robert
  • 5,038
  • 1
  • 25
  • 43