【问题标题】:Nested foreach %dopar% in RR中的嵌套foreach %dopar%
【发布时间】:2019-01-29 19:56:55
【问题描述】:

所以我已经成功地在 R 中使用了 foreach %dopar% 和一个循环(比如“i”)。现在我正在用两个循环(比如“i”和“j”)尝试 foreach。

我需要在两个大型数据框之间运行相关性分析。 DF1 有 22 个特征和 280 个样本(值“i”循环通过该 DF 的行)。 DF2 有大约 20,000 个特征和相同的 280 个样本(值“j”循环通过此 DF 的行)

从我的调试中,我了解到我编写的这段代码只在 i 和 j 具有相同值的循环中运行。 i,e, 对于 1 和 j = 1, 2,....22。

对于其他 j 值,循环没有运行。有人可以帮我弄清楚如何编辑代码以使其适用于所有“i”和“j”值吗?非常感谢!

我的代码如下。

corrResult1<-foreach(i=1:nrow(DF1),j=1:nrow(DF2),.combine=rbind) %dopar% {
oneValueDF1 = as.numeric(DF1[i,])
oneValueDF2 = as.numeric(DF2[j,])
myCor1 = cor(x = oneValueDF1, y = oneValueDF2 , method = "pearson")
    #correlation test
myCor1_test = cor.test(oneValueDF2, oneValueDF1, method="pearson") 

tempMatrix = cbind(oneValueDF1, oneValueDF2,
                       myCor1_test$statistic, # T stat
                       myCor1_test$parameter, #DF
                       myCor1_test$p.value, #p alue
                       myCor1_test$estimate, #corr coeff
                       myCor1_test$conf.int[1], #conf int 1
                       myCor1_test$conf.int[2], #conf int 2
                       myCor1_test$null.value, 
                       myCor1_test$alternative,
                       myCor1_test$method) 

    tempMatrix 

}

【问题讨论】:

  • 我正在尝试这个。让我们看看它是否有效: corrResult1

标签: r foreach


【解决方案1】:

这行得通。如果它对任何人都有帮助:

corrResult1<-foreach(i=1:nrow(DF1),.combine=rbind) %:% 
             foreach(j=1:nrow(DF2),.combine=rbind) %dopar% { 

oneValueDF1 = as.numeric(DF1[i,])
oneValueDF2 = as.numeric(DF2[j,])
myCor1 = cor(x = oneValueDF1, y = oneValueDF2 , method = "pearson")
#correlation test
myCor1_test = cor.test(oneValueDF2, oneValueDF1, method="pearson") 

tempMatrix = cbind(oneValueDF1, oneValueDF2,
                   myCor1_test$statistic, # T stat
                   myCor1_test$parameter, #DF
                   myCor1_test$p.value, #p alue
                   myCor1_test$estimate, #corr coeff
                   myCor1_test$conf.int[1], #conf int 1
                   myCor1_test$conf.int[2], #conf int 2
                   myCor1_test$null.value, 
                   myCor1_test$alternative,
                   myCor1_test$method) 

    tempMatrix 

         }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2018-07-13
    • 2020-09-04
    • 2019-09-27
    • 1970-01-01
    • 2016-08-02
    • 2015-02-27
    • 2011-04-23
    相关资源
    最近更新 更多