在使用R中的mapply时,如何将t.test的输出保存到数据框中?

4

I have two dataframes of the sort

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

使用相同的列名。我正在使用

mapply(t.test, df1, df2)

将比较两个数据框中的a、b和c类型列的t.text结果输出。这将产生(实际结果):

        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]]"

等等(在每个数据帧中我有大约180列数据)。 我需要将列名及其对应的p值存储在矩阵中。 在另一列中存储哪个数据帧包含更高的值也会很有帮助。 请帮忙?


t.tests的结果实际上存储在一个列表中,因此您可以使用访问器$p.value进行调用。 - daikonradish
1个回答

5

试试这个:

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 

如果您想要一个两列格式的data.frame,可以使用stack来包装它,就像这样:

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

如果你想除了p值以外还要保存t统计量,该怎么做呢? - Sheila

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接