r-列表之间的嵌套%dopar%循环

时间:2019-05-09 12:20:54

标签: r list loops foreach parallel-processing

我需要在R中的两个列表之间执行嵌套的%dopar%循环。

我使用非并行代码处理循环,如下所示:

main_lst = rep(list(list()), 10) # create main list where loop's results will be stored

lst_1 = rep(list(list()), 25) # create list no. 1

for (i in 1:length(lst_1)) {
  lst_1[[i]] = data.frame(x = seq(1:30), y = rnorm(30))
}

lst_2 = rep(list(list()), 10) # create list no. 2

for (i in 1:length(lst_2)) {
  lst_2[[i]] = data.frame(x = seq(16:30), z = rnorm(15))
}


#### Do the for loop (non parallelised)
for (h in 1:length(main_lst)) {
  for (i in 1:length(lst_1)) {

    main_lst[[h]][[i]] = merge(lst_1[[i]], lst_2[[h]][,c(1:2)], by = 'x')

  }
}

关于如何并行处理上述for循环的任何建议? 我应该试试lapply(或parlapply)吗?

这是我尝试过的方法,但是不起作用:

### Run in Parallel
library(foreach)
library(doParallel)

#setup parallel backend to use many processors
cores=detectCores()
cl = makeCluster(cores[1]-1) 
registerDoParallel(cl)


main_lst = foreach(h=1:length(main_lst)) %:% {
  foreach(i=1:length(lst_1)) %dopar% {

    main_lst[[h]][[i]] = merge(lst_1[[i]], lst_2[[h]][,c(1:2)], by = 'x')

  }
}


#stop cluster
stopCluster(cl)
  

foreach(h = 1:main_lst)%:%{:“%:%”传递了错误   非法权限操作数

1 个答案:

答案 0 :(得分:2)

我只编辑了少数几个(删除了{},更改了h的迭代限制并分配了main_lst)

main_lst = foreach(h=1:10) %:% 
  foreach(i=1:length(lst_1)) %dopar% {
    merge(lst_1[[i]], lst_2[[h]][,c(1:2)], by = 'x')
   }

foreach语句的结果将自动按列表收集(除非您设置.combine = rbind之类的特定类型。)

所以您不必分配它!