I am trying to use foreach in a given dataframe with a defined function.
Could anyone suggest me especially on lines 41-42 in the following code?
i.e.
finalSum <- foreach(b=iter(batchSets, by='row'), .combine=rbind) %dopar%
fun_1(x=dat1$first,y=dat1$second,i=dat1$sl)
The code:
require(microbenchmark, quietly=TRUE)
require(doParallel, quietly=TRUE)
require(ggplot2, quietly=TRUE)
detectedCores <- parallel::detectCores()
registerDoParallel(cores=detectedCores - 1)
#Given dataframe
set.seed(10)
dat1=data.frame(
sl=1:10,
first=sample(10),
second=sample(10))
# Function to add a row in each row
# I need in this way by extracting a row at a time
fun_1 <- function(x,y,i){
df_mont=dat1 %>%
filter(sl==i)
df= df_mont %>% add_row(sl=i,first = x, second = y)
df
}
# Example of running the function with mapply
output=mapply(function(x,y,i) fun_1(x,y,i),
x=dat1$first,y=dat1$second,
i=dat1$sl,
SIMPLIFY = FALSE)
# Expected output
output1 <- do.call("rbind", output)
#Required with foreach
# I want to do it using foreach
parll<- function(x) {
items <- nrow(x)
batches <- detectedCores * 4
batchSets <- split(x, rep(1:batches, length.out=items))
finalSum <- foreach(b=iter(batchSets, by='row'), .combine=rbind) %dopar%
fun_1(x=dat1$first,y=dat1$second,i=dat1$sl)
return (finalSum)
}
parll(x=dat1)