我在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给我下标错误。
最佳答案
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/
关于r - 平行于回路R,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20239707/