我想对R中的数据执行双样本t检验。 给定两个高维数据帧,我需要以某种方式循环遍历所有行的匹配列(字符串中的字符串colnames())并对每个列对执行测试 - 分别来自df1和df2。问题是数据帧中的列不是正确的顺序,即col1形式df1与df2中的col1不匹配,df2具有df1中不存在的其他列。我从来没有使用R来完成这些任务,我想知道是否有一个快速而方便的解决方案来在t测试的数据帧中找到匹配的列对。
我考虑过for循环,但我认为这对于大数据帧来说效率非常低。
提前感谢您的帮助。
* EDITED -------两个小例子数据帧,df1和df2 --------------------------- -----
**** **** DF1
"Row\Column" "A2" "A1" "A4" "A3"
"id_1" 10 20 0 40
"id_2" 5 15 25 35
"id_3" 8 0 12 16
"id_4" 17 25 0 40
**** **** DF2
"Row\Column" "A3" "A8" "A5" "A6" "A1" "A7" "A4" "A2"
"id_1" 0 2 0 4 0 1 2 3
"id_2" 1 5 8 3 4 5 6 7
"id_3" 2 10 6 9 8 9 10 11
"id_4" 7 2 10 2 55 0 0 0
"id_5" 0 1 0 0 9 1 3 4
"id_6" 8 0 1 2 7 2 3 0
匹配列只是df1中的列名与df2中的列名匹配。 例如 df1和df2中的两个匹配列是e。 G。 “A1”和“A1”,“A2”和“A2”......你明白了......
答案 0 :(得分:4)
mapply
是您正在寻找的功能。
如果您data.frame
的列匹配,则只需使用
mapply(t.test, df1, df2)
但是,由于他们不这样做,您需要确定df1
的哪一列与df2
的哪一列相关。幸运的是,R
中的索引选项很聪明,如果您输入列名称的向量(集合),您将按给定的顺序返回这些列。这让生活变得轻松。
# find the matching names
## this will give you those names in df1 that are also in df2
## and *only* such names (ie, strict intersect)
matchingNames <- names(df1)[names(df1) %in% names(df2)]
请注意matchingNames
有一些顺序
现在看看当你使用matchingNames
向量作为每个df1和df2的列的索引时会发生什么(还要注意列顺序)
df1[, matchingNames]
df2[, matchingNames]
matchingNames
因此,我们现在有两个data.frames具有匹配正确的列,我们可以使用它们来mapply
。
## mapply will apply a function to each data.frame, one pair of columns at a time
## The first argument to `mapply` is your function, in this example, `t.test`
## The second and third arguments are the data.frames (or lists) to simultaneously iterate over
mapply(t.test, df1[, matchingNames], df2[, matchingNames])
答案 1 :(得分:0)
如果没有可重复的示例,很难给出一个好的答案。您还需要定义matching
列的含义。
这里有一个2个data.frames的例子,它们有一些共同的列名。
df1 <- matrix(sample(1:100,5*5,rep=TRUE),ncol=5,nrow=5)
df2 <- matrix(sample(1:100,5*8,rep=TRUE),ncol=8,nrow=5)
colnames(df1) <- letters[6:10]
colnames(df2) <- rev(letters[1:8])
然后我定义了一个t.test
的包装器,以限制例如p值的输出和自由度。
f <- function(x,y){
test <- t.test(x,y)
data.frame(df = test$parameter,
pval = test$p.value)
}
然后使用sapply
我迭代使用intersect
sapply(intersect(colnames(df1),colnames(df2)),
function(x) f(df1[,x], df2[,x]))
f g h
df 7.85416 6.800044 7.508915
pval 0.5792354 0.2225824 0.4392895