【问题标题】:column paired statistic test列配对统计检验
【发布时间】:2013-03-11 22:52:43
【问题描述】:

我有两个 data.frames,看起来像:

DF1      
  Col1     Col2     Col3    Col4    
 0.1854   0.1660   0.1997   0.4632
 0.1760   0.1336   0.1985   0.4496
 0.1737   0.1316   0.1943   0.4446    
 0.1660   0.1300   0.1896   0.4439


DF2       
  Col1     Col2     Col3    Col4    
 0.2456    0.2107   0.2688  0.5079
 0.2399    0.1952   0.2356  0.1143
 0.2375    0.1947   0.2187  0.0846    
 0.2368    0.1922   0.2087  0.1247

我想在两个 data.frames 之间执行 wilcox.test,特别是在成对的列之间,这样:

test1: between Col1 of DF1 and Col1 of DF2     
test2: between Col2 of DF1 and Col2 of DF2      

等等。

我使用了以下脚本:

for (i in 1:length(DF2)){ 
    test <- apply(DF1, 2, function(x) wilcox.test(x, as.numeric(DF2[[i]]), correct=TRUE))
}

不幸的是,此脚本的输出与使用以下脚本执行的相同测试的输出不同:

test1 = wilcox.test(DF1[,1], DF2[,1],  correct=FALSE)     
test2 = wilcox.test(DF1[,2], DF2[,2],  correct=FALSE)       

由于在真实的 data.frames 中我有大约 100 列和 200 行(它们在维度上是相等的),所以我不能按列制作测试列。

dput(DF1)之后:

structure(list(Col1 = c(0.1854, 0.1760, 0.1737, 0.1660,....),  class = "data.frame", row.names = c(NA, -100L)))

DF2 也一样

【问题讨论】:

  • 您能否通过显示dput(DF1)dput(DF2) 以可重现的格式显示数据帧?

标签: r dataframe


【解决方案1】:

这是一个经典的mapply 案例——基本上只是sapply 的多变量版本。我们使用mapply依次遍历每个数据帧。首先,创建一些数据:

df1 = data.frame(c1 = runif(10), c2 = runif(10), c3 = runif(10), c4 = runif(10))
df2 = data.frame(c1 = runif(10), c2 = runif(10), c3 = runif(10), c4 = runif(10))

然后使用mapply

l = mapply(wilcox.test, df1, df2, SIMPLIFY=FALSE, correct=FALSE)

这里的变量l 是一个列表。所以,

wilcox.test(df1[,1], df2[,1],  correct=FALSE) 
l[[1]]
wilcox.test(df1[,2], df2[,2],  correct=FALSE) 
l[[2]]

【讨论】:

    【解决方案2】:

    使用 for 循环来遍历列名可能更容易

    for (name in colnames(DF2)){
        ...
        wilcox.test(DF1[,name], DF2[,name],  correct=FALSE))
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 2015-01-21
      相关资源
      最近更新 更多