【问题标题】:Wilcox.test between data frames in RR中数据帧之间的Wilcox.test
【发布时间】:2015-11-01 17:20:47
【问题描述】:

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

【问题讨论】:

    标签: r


    【解决方案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
    

    【讨论】:

    • @LysandeR 嗨,我是一个绝对的初学者,我不能完全跟随。数据帧具有以下维度: > dim(A) [1] 700 150 > dim(B) [1] 700 50 我尝试使用 lapply 函数和替换的数据帧名称,我得到:错误:意外'}' in " lapply(1:nrow(A), function(i) {wilcox.test(as.numeric(A[i, ]), as.numeric(B[i, ])$p.value}"
    • 您缺少两个括号......lapply(1:nrow(A), function(i) {wilcox.test(as.numeric(A[i, ]), as.numeric(B[i, ]))$p.value})。试试这个,它会工作的。每当您看到unexpected } or ) 时,就意味着您忘记关闭括号了。
    猜你喜欢
    • 2022-01-10
    • 2018-09-27
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多