我刚接触使用foreach并行执行for循环,对它的工作原理感到困惑。举个练习的例子,我根据一个数据框(input)创建了一个简单的列表(input2),尝试通过遍历h和j来计算b。
library(doParallel)
library(foreach)
library(dplyr)
input <- data.frame(matrix(rnorm(200*200, 0, .5), ncol=200))
input[input <=0] =0
input['X201'] <- seq(from = 0, to = 20, length.out = 10)
input <- input %>% select(c(X201, 1:200))
input2 <- split(input, f= input$X201)
a = 0
b= 0
cl <- parallel::makeCluster(20)
doParallel::registerDoParallel(cl)
tm1 <- system.time(
y <-
foreach (h = length(input2),.combine = 'cbind') %:%
foreach (j = nrow(input2[[h]]),.combine = 'c',packages = 'foreach') %dopar%{
a = input2[[h]][j,3]
b = b + a
}
)
parallel::stopCluster(cl)
registerDoSEQ()
print("Cluster stopped.")
y的值约为0.55(确切值取决于所生成的随机数),这是input2[[10]][20,3]的值,而不是我所期望的累积值。我查看了foreach包的手册,但仍不确定是否完全理解foreach函数的机制。
dplyr
、parallel
和foreach
,但请在示例代码中明确说明。 - r2evansforeach
基本上像lapply
一样工作。它使用表达式而不是函数,语法也不同,但原理完全相同,即没有副作用,如在循环外更新变量。所需(由赏金)的官方来源是包文档。 - Roland