I have a function containing a loop
myfun = function(z1.d, r, rs){ x = z1.d[,r] or.d = order(as.vector(x), decreasing=TRUE)[rs] zz1.d = as.vector(x) rl = zz1.d[or.d] y=vector() for (i in 1:9) { if(i<9) y[i]=mean( x[(x[,r] >= rl[i] & x[,r] < rl[i+1]),r] ) else{ y[i] = mean( z1.d[(x >= rl[9]),r] )} } return(y) }
rs is a number vector, z1.d is a zoo, and y is also a number vector.
When I try to run a function inside a parallel loop:
cls = makePSOCKcluster(8) registerDoParallel(cls) rlarger.d.1 = foreach(r=1:dim(z1.d)[2], .combine = "cbind") %dopar% { myfun(z1.d, r, rs)} stopCluster(cls)
I get the following error:
Error in { : task 1 failed - "incorrect number of dimensions"
I do not know why, but I realized that if I exit the loop from my function, this will not give an error.
Also, if I run the same code with% do% instead of% dopar% (therefore it does not run in parallel), it works fine (slow, but without errors).
EDIT: as requested here is an example of parameters:
dim(z1.d) [1] 8766 107 > z1.d[1:4,1:6] AU_10092 AU_10622 AU_12038 AU_12046 AU_13017 AU_14015 1966-01-01 23:00:00 NA NA NA 1.816 0 4.573 1966-01-02 23:00:00 NA NA NA 9.614 0 4.064 1966-01-03 23:00:00 0 NA NA 0.000 0 0.000 1966-01-04 23:00:00 0 NA NA 0.000 0 0.000 > rs [1] 300 250 200 150 100 75 50 30 10
r is defined in the foreach loop