R

时间:2015-11-01 17:20:48

标签: r

我想将wilcox.test应用于R中两个数据帧的每一行。例如,df1中的第1行和df2中的第1行,以查看它们是否显着不同。我有数百行,并期望数百个P值成为结果。有105列。我不太确定如何编写一个为我的每个行对进行测试的命令,因为它有数百个。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

以下列数据为例:

#2 numeric data.frames (all columns are numeric)
#5 rows and 100 columns
set.seed(5)
df1 <- as.data.frame(matrix(runif(500), nrow=5, ncol=100))
df2 <- as.data.frame(matrix(runif(500), nrow=5, ncol=100))

解决方案

#A single lapply is enough to run the wilcox test for each row
lapply(1:nrow(df1), function(i) {
  #you run the wilcox.test for each pair of rows and return the p.value
  wilcox.test(as.numeric(df1[i, ]), as.numeric(df2[i, ]))$p.value
})

输出:

> lapply(1:nrow(df1), function(i) {
+ wilcox.test(as.numeric(df1[i, ]), as.numeric(df2[i, ]))$p.value
+ })
[[1]]
[1] 0.8690001

[[2]]
[1] 0.1390142

[[3]]
[1] 0.7479788

[[4]]
[1] 0.5340455

[[5]]
[1] 0.8459806