soosus
soosus

Reputation: 1217

Saving output of t.test to dataframe when using mapply in R

I have two dataframes of the sort

a    b    c
1    2    3
2    3    3
1    2    3

with the same column names. I'm using

mapply(t.test, df1, df2)

to output the results of a t.text comparing the a,b, and c-type columns between the two dataframes. This yields (actual results):

        LHM                                      
statistic   -11.5671                                 
parameter   90.46322                                 
p.value     1.575918e-19                             
conf.int    Numeric,2                                
estimate    Numeric,2                                
null.value  0                                        
alternative "two.sided"                              
method      "Welch Two Sample t-test"                
data.name   "dots[[1L]][[23L]] and dots[[2L]][[23L]]"
        RaM                                      
statistic   -18.66368                                
parameter   172.2032                                 
p.value     3.200675e-43                             
conf.int    Numeric,2                                
estimate    Numeric,2                                
null.value  0                                        
alternative "two.sided"                              
method      "Welch Two Sample t-test"                
data.name   "dots[[1L]][[24L]] and dots[[2L]][[24L]]"

etc. etc. (I have ~180 columns of data in each dataframe). I need to store the names of the columns and their corresponding p-values in a matrix. It would also be helpful to store which dataframe contained the higher value in another column. Help?

Upvotes: 4

Views: 2831

Answers (1)

Arun
Arun

Reputation: 118879

Try this:

mapply(function(x, y) t.test(x,y)$p.value, df1, df2)
# on my sample(/dummy) data.frames
#           a           b           c 
# 0.009963864 0.009963864 0.020204103 

You can wrap it up with stack if you want a 2 column format data.frame like so:

stack(mapply(function(x, y) t.test(x,y)$p.value, df1, df2))
#        values ind
# 1 0.009963864   a
# 2 0.009963864   b
# 3 0.020204103   c

Upvotes: 5

Related Questions