[R] foreach {parallel} nested with for loop to update data.frame column

Alexander.Herr at csiro.au Alexander.Herr at csiro.au
Thu Aug 4 03:32:14 CEST 2016


Hi List,

Trying to update a data.frame column within a foreach nested for loop

### trial data
set.seed(666)
xyz<-as.data.frame(cbind(x=rep(rpois(5000,10),2)+1, y=rep(rpois(5000,10),2)+1,z=round(runif(10000, min=-3, max=40),2)))
xyz$mins<-rep(NA, nrow(xyz)) 

cl<-makeCluster(16)  #adjust to your cluster number
registerDoParallel(cl)

counter=0
foreach(i=unique(xyz[,1]), .combine=data.frame, .verbose=T) %dopar% {
        for( j in unique(xyz[,2])) {
                xyz[xyz[,2] == j ,4]<-min(xyz[xyz[,2] == j ,2]) 
        }

}

stopCluster(cl)

This is obviously not working. Any hints?

Thanx
Herry



More information about the R-help mailing list