如何在R中的dataframe中对列运行t-test?
我有6列数字数据的dataframe。我想对表单中的列对进行t检验;第一个+第二个列,第三个+第四个列,第五个+第六个列,直到我有3个t-testp值,可以存储在新的数据帧中 我知道我可以使用select()获取列,并且可以使用t.test(x,y)$p.values提取p值。但我想不出一个好方法来在for循环中自动实现这一点 我想做这样的事情:如何在R中的dataframe中对列运行t-test?,r,loops,R,Loops,我有6列数字数据的dataframe。我想对表单中的列对进行t检验;第一个+第二个列,第三个+第四个列,第五个+第六个列,直到我有3个t-testp值,可以存储在新的数据帧中 我知道我可以使用select()获取列,并且可以使用t.test(x,y)$p.values提取p值。但我想不出一个好方法来在for循环中自动实现这一点 我想做这样的事情: p-values <- for (i in select(dataframe,-1) t.test(i,?)$p.value p值快速回答如下
p-values <- for (i in select(dataframe,-1) t.test(i,?)$p.value
p值快速回答如下:
df <- matrix(rnorm(18), nrow = 3, ncol = 6)
p <- ncol(df)
indices <- cbind(seq(1, p, 2), seq(2, p + 1, 2))
for(i in 1:(p/2)){
print(t.test(df[,indices[i,]])$p.value)
}
df快速回答如下:
df <- matrix(rnorm(18), nrow = 3, ncol = 6)
p <- ncol(df)
indices <- cbind(seq(1, p, 2), seq(2, p + 1, 2))
for(i in 1:(p/2)){
print(t.test(df[,indices[i,]])$p.value)
}
df像Rémi Coulaud那样创建列索引是一个很好的方法。以下是创建相同索引并直接返回成对p值的变体:
df <- as.data.frame(matrix(rnorm(36), nrow = 6, ncol = 6))
indices <- matrix(seq_len(ncol(df)), nrow = 2)
p.values <- apply(indices, 2, function(idx) t.test(df[idx])$p.value)
df像Rémi Coulaud那样创建列索引是一个很好的方法。以下是创建相同索引并直接返回成对p值的变体:
df <- as.data.frame(matrix(rnorm(36), nrow = 6, ncol = 6))
indices <- matrix(seq_len(ncol(df)), nrow = 2)
p.values <- apply(indices, 2, function(idx) t.test(df[idx])$p.value)
df