0

I would like to apply T test in R within a loop

Groups  Length  Size    Diet    place
A   2.4048381   0.7474989   1.6573392   334.3273456
A   2.72500485  0.86392165  1.8610832   452.5593152
A   1.396782867 0.533330367 0.8634525   225.5998728
B   1.3888505   0.46478175  0.92406875  189.9576476
B   1.38594795  0.60068945  0.7852585   298.3744962
B   2.53491245  0.95608005  1.5788324   303.9052525

I tried this code with loop, but it is not working:

for (i in 2:4){
  t.test(table[,c(i)] ~ table$Groups, conf.level = 0.95)
}

Can anyone help me with this? Thanks!

Jontexas
  • 121
  • 8
  • I don't actually get an error even if my data frame is called `table`. But generally, [don't use R's function as variable names](http://stackoverflow.com/questions/11308367/error-in-my-code-object-of-type-closure-is-not-subsettable) as @zheyuan-li said. Maybe tell us the error you get from R. – lao_zhang Nov 06 '16 at 07:26
  • 1
    There is no error message , but also it results in nothing heheheh – Jontexas Nov 06 '16 at 16:03

2 Answers2

1

Your code computes 4 t-tests, but the results are lost, because you don't do anything with them. Try the following:

info <- read.table(header=TRUE, text="Groups  Length  Size    Diet    place
               A   2.4048381   0.7474989   1.6573392   334.3273456
               A   2.72500485  0.86392165  1.8610832   452.5593152
               A   1.396782867 0.533330367 0.8634525   225.5998728
               B   1.3888505   0.46478175  0.92406875  189.9576476
               B   1.38594795  0.60068945  0.7852585   298.3744962
               B   2.53491245  0.95608005  1.5788324   303.9052525")

results <- list()
for (i in 2:4){
    results[[i]] <- t.test(info[,i] ~ info$Groups, conf.level = 0.95)
}

print(results)

When interacting with the REPL/console, typing the t.test function will compute results and return them. The console will print everything that is returned. In scripts that you source, the t.test function will return results but they wil not be printed. This is why I put them into a list and printed the list later on.

Btw, I stored your information as info not as table. R will deal great with variable names that are also function names, but every now and then you will hava a hard time to read error messages, so avoid naming variables table or matrix or c or df.

Bernhard
  • 4,272
  • 1
  • 13
  • 23
  • Perfect explanation @Bernhard! It worked very well and as it has few commands I can add more collumns and data :) – Jontexas Nov 06 '16 at 16:14
1

Using apply functions you could also do:

res<- cbind(
do.call(rbind,apply(info[,-1],2,function(cv)t.test(cv ~ info$Groups, conf.level = 0.95)
                    [c("statistic","parameter","p.value")]))
,
t(apply(info[,-1],2,function(cv)unlist(t.test(cv ~ info$Groups, conf.level = 0.95)
                    [c("conf.int","estimate")])))
)
res


> res
       statistic parameter p.value   conf.int1  conf.int2 estimate.mean in group A estimate.mean in group B
Length 0.7327329 3.991849  0.5044236 -1.13263   1.943907  2.175542                 1.769904                
Size   0.2339013 3.467515  0.8282072 -0.47739   0.5595231 0.714917                 0.6738504               
Diet   0.9336103 3.823748  0.4056203 -0.7396173 1.468761  1.460625                 1.096053                
place  0.9748978 3.162223  0.398155  -159.4359  306.2686  337.4955                 264.0791
Robert
  • 5,038
  • 1
  • 25
  • 43