0

I'm new to parallel-processing and attempting to parallelize a for loop in which I create new columns in a data frame by matching a column in said data frame with two other data frames. j, the data frame I'm attempting to create columns in is 400000 x 54. a and c, the two data frames I'm matching j with are 5000 x 12 and 45000 x 8 (respectively).

Below is my initial loop prior to the attempt at parallelizing:

for(i in 1:nrow(j)) {
   if(j$Inspection_Completed[i] == TRUE) {
      next
  }

  j$Assigned_ID <- a$Driver[match(j$car_name, a$CarName)]

  j$Title <- c$Title[match(j$Site_ID, c$LocationID)]

  j$Status <- c$Status[match(j$Site_ID, c$LocationID)]    
 

}

So far I have attempted the following:

cl <- snow::makeCluster(4)
doSNOW::registerDoSNOW(cl)

foreach::foreach(i = 1:nrow(j)) foreach::`%dopar%` {
   if(j$Inspection_Completed[i] == TRUE) {
      next
   }

  j$Assigned_ID <- a$Driver[match(j$car_name, a$CarName)]

  j$Title <- c$Title[match(j$Site_ID, c$LocationID)]

  j$Status <- c$Status[match(j$Site_ID, c$LocationID)] 
} 
stopCluster(cl)

However, when I run the code above I receive several errors.

Error: unexpected symbol in "foreach::foreach(i = 1:nrow(j)) foreach"

And:

Error: object 'i' not found

Lastly:

Error: unexpected '}' in "}"

I'm not sure why I'm getting these errors. None of the columns in any of the data frames are factors and I haven't been able to spot any mismatched parentheses or brackets. I've also done this without the snow and doSNOW packages and the result is the same. I've ran it without the tick marks around dopar as well with the same result.

3
  • The function name is %dopar%, not dopar. Try foreach::foreach(i = 1:nrow(j)) foreach::%dopar% .... Commented May 3, 2021 at 0:14
  • Not sure if you were thinking this, but just in case ... you cannot wrap a function in % and expect it to become an infix function. Quite the opposite, a function needs to be defined as being wrapped in % for R to use it as an infix operator. Commented May 3, 2021 at 0:17
  • @r2evans the absence of % with dopar was a typo on my part. Even with %dopar% the errors are the exact same. Commented May 3, 2021 at 0:57

1 Answer 1

1

(I didn't know this before.)

R doesn't like infix operators with the ::-notation. Even if you're doing that for namespace management, R isn't having it:

1L %in% 1:2
# [1] TRUE
1L base::%in% 1:2
# Error: unexpected symbol in "1L base"
1L base::`%in%` 1:2
# Error: unexpected symbol in "1L base"

Workarounds:

  1. Redefine your own infix that just mimics the other, as in

    `%myin%` <- base::`%in%`
    1L %myin% 1:2
    # [1] TRUE
    
  2. Use explicit namespace inclusion with library(foreach) before that point in your code, and just use %dopar%. (Not that it helps much, but using library(foreach) does not mean you cannot use foreach::foreach, though it is unnecessary.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.