3

我在 for 循环中调用一个函数。

sample_fun <- function(x,y) {
    # do something with x,y and return value
}

My loop looks like
result = list()

for(i in 1:10000)
   result[i] = sample_fun(i,i+1)

我如何并行化这个循环。使用 foreach 和 dopar 会给我下标错误。

4

1 回答 1

7
library(doSNOW)

sample_fun <- function(x,y) {
  # do something with x,y and return value
}

threads <- 8
N <- 10000

cl <- makeCluster(threads)
registerDoSNOW(cl)

result <- foreach(i=1:N) %dopar% sample_fun(i,i+1)

stopCluster(cl)

http://www.joyofdata.de/blog/parallel-computing-r-windows-using-dosnow-foreach/

于 2013-11-27T10:24:55.240 回答