Skip to content
Prev 363154 / 398502 Next

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

Hiya,

This now works...

test<-foreach(i=unique(xyz[,1]), .combine=rbind, .verbose=T) %dopar% {
         for( j in unique(xyz[,2])) {
         xyz[xyz[,2] == j & xyz[,1] == i ,4]<-min(xyz[xyz[,2] == j & xyz[,1] == i,3]) 
	nr=nrow(xyz[xyz[,2] == j & xyz[,1] == i ,4])
        }
        return(xyz[xyz[,1]== i,])  # you must return what you are farming out...
}
head(test)

-----Original Message-----
From: Herr, Alexander Herr - Herry (L&W, Black Mountain)
Sent: Friday, 5 August 2016 9:29 AM
To: 'R-Help (r-help at stat.math.ethz.ch)' <r-help at stat.math.ethz.ch>
Subject: Re: foreach {parallel} nested with for loop to update data.frame column 

Hiya,
Yes that would work, also an aggregate with merge can work, but I really would like to make this a parallel calculation with farming out the first loop to different workers and put the output together again into the data.frame with additional columns. This will speed up work with very large files and avoid running out of memory issues

Cheers
H
XXXXXXXXXXXXXXXXXXXXXX Petr wrote XXXXXXXXXXXXXXXXXXXXXXXXXXXX Hi 

I may be completely wrong but isn't it work for ave? With your example I get
? ?x ?y ? ? z ?mins
1 13 15 ?1.97 -2.91
2 17 ?9 14.90 -2.81
3 ?9 10 34.68 -1.97
4 17 ?6 ?4.26 -2.63
5 ?3 12 ?0.12 ?0.12
6 19 11 ?7.91 ?7.91
Cheers
Petr