Home > Back-end >  Optimize and append values to dataframe with foreach R
Optimize and append values to dataframe with foreach R

Time:09-22

I want to optimize my nested for loops by parallelizing them with the foreach package. I'm new to this subject, and I tried several approaches to append the values to a data frame. I know that something is off with the for loops as it returns a value, but I want to combine those values and store them into a data frame. I tried to replace the for loop iterating over the rows with a foreach loop, but I couldn't make it work. The idea is to get a better understanding of parallelizing for loops.

library(foreach)
library(doParallel)

cl <- makeCluster(2)
registerDoParallel(cl)

df <- data.frame(x=sample(10), y=sample(10), z = sample(10))
repeats <- 2

FUN <- function(df, repeats) {

   foreach(k=1:repeats, .combine = 'rbind') %dopar% {

       for(i in 1:nrow(df)) {
  
          for(j in 1:ncol(df)) {
    
             c(k=k ,i=i ,j=j , value=df[i, 1] * j )
    
          }
       }
    } 
}


FUN(df, repeat)
stopCluster(cl)

As mentioned before, I tried to replace the for loop with foreach. Again, the values are not stored properly.

foreach(k=1:repeats, .combine = 'rbind') %:% {

       foreach(i=1:nrow(df), .combine = 'c') %dopar%{
  
          for(j in 1:ncol(df)) {
    
             c(k=k ,i=i ,j=j , value=df[i, 1] * j )
    
          }
       }
    } 

Example of the output

  k i j value
  1 1 1 3
  1 1 2 6

CodePudding user response:

foreach has a special syntax for nested loops (see vignette("nested"):

FUN <- function(df, repeats) {
  
  foreach(k=1:repeats, .combine = 'rbind') %:%
    foreach(i = 1:nrow(df), .combine = 'rbind') %:%
    foreach(j = 1:ncol(df),  .combine = 'rbind') %dopar% {
      
      c(k=k ,i=i ,j=j , value=df[i, 1] * j )
      
    }
}
`rownames<-`(FUN(df, repeats), NULL)

My output is longer than yours, so I'm not sure what you were going for. Also, whenever you provide an example using random data, use set.seed. So we all get the same result every time.

  • Related