R:在数据帧的一列上运行t-test,而不是在所有其他列上运行t-test
我有一个基本形式的数据框架:R:在数据帧的一列上运行t-test,而不是在所有其他列上运行t-test,r,statistics,dataframe,R,Statistics,Dataframe,我有一个基本形式的数据框架: > head(raw.data) 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
> head(raw.data)
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值保存到以下变量中:
#run tests
test.result = mapply(t.test, one.column, other.columns)
#store p-values
p.values = stack(mapply(function(x, y)
+ t.test(x,y)$p.value, one.column, other.columns))
或者aov()是进行此类分析的更好选择?在任何情况下,我都想知道如何使用t测试简化操作 这里有一个解决方案:
读入数据:
dat <- read.table(text='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')
更好的解决方案是使用rehape包装中的熔体,并进行配对试验:
library(reshape)
with(melt(dat), pairwise.t.test(value, variable, p.adjust.method = 'none'))
如果只想将第一列与所有其他列配对,也可以使用以下方法:
x <- sapply(dat[,-1], function(x) t.test(x, dat[,1])$p.value)
data.frame(variable = names(x), p.value = as.numeric(x))
x感谢您的详细回复!我试过了,效果很好。但是,我正在寻找一种解决方案,它不只是将列1与列1配对,而是为每个可能的列将列1与其余列配对。换句话说,第1栏与第2-n栏、第2栏与第1,3-n栏等等。我现在编辑了我的帖子,并在最后提供了另一个解决方案。我希望这有帮助。
data.frame(comparison = paste(com[1,], com[2,], sep = ' vs. '), p.value = p)
library(reshape)
with(melt(dat), pairwise.t.test(value, variable, p.adjust.method = 'none'))
x <- sapply(dat[,-1], function(x) t.test(x, dat[,1])$p.value)
data.frame(variable = names(x), p.value = as.numeric(x))