在单个列和给定数据帧的其余部分之间的R中进行t检验

时间:2013-04-30 18:48:11

标签: r statistics dataframe

这是在R.我需要帮助获取基本格式的数据框

       NAC     cOF3     APir       Pu       Tu     V2.3     mOF3     DGpf
1 6.314770 6.181188 6.708971 6.052134 6.546938 6.079848 6.640716 6.263770
2 8.825595 8.740217 9.532026 8.919598 8.776969 8.843287 8.631505 9.053732
3 5.518933 5.982044 5.632379 5.712680 5.655525 5.580141 5.750969 6.119935
4 6.063098 6.700194 6.255736 5.124315 6.133631 5.891009 6.070467 6.062815
5 8.931570 9.048621 9.258875 8.681762 8.680993 9.040971 8.785271 9.122226
6 5.694149 5.356218 5.608698 5.894171 5.629965 5.759247 5.929289 6.092337

并简化了对每个列进行t检验的任务与所有其他列相结合的任务。我还需要p值,我计划通过以下方式获得:

#t-test
test.result = mapply(t.test, select.column, other.columns)
#store p-values
p.values = stack(mapply(function(x, y) t.test(x,y)$p.value, select.column, other.columns))

或者aov()会成为这种分析的更好选择吗?

1 个答案:

答案 0 :(得分:2)

sapply(names(dat), function(x) 
         sapply( names(dat), function(y) t.test(dat[[x]],dat[[y]])$statistic ))

可以使用函数t.test中列表中返回的任何值。也可以遍历combn(names(dat), 2)的结果,这将是choose(n,2)矩阵。 (方阵具有明显的冗余结果)显然需要对多次测试进行合理调整。可以使用p.adjust