I am able to use lapply when the list has single observations.I am trying the same over multiple observations. However, the same code got an error with
1: In sl == i :
longer object length is not a multiple of shorter object length
when the list has multiple values.
Expected outcome:
#mapply#################
#Given dataframe
set.seed(10)
dat1=data.frame(
sl=1:17,
first=sample(17),
second=sample(17))
# 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+0.5,first = x+2, second = y+3)
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)
lapply over the same function:
#####################################################
require(microbenchmark, quietly=TRUE)
require(doParallel, quietly=TRUE)
require(ggplot2, quietly=TRUE)
detectedCores <- parallel::detectCores()
registerDoParallel(cores=detectedCores - 1)
fun_2 <- function(z){
i=z$sl
x=z$first
y=z$second
df_mont=dat1 %>%
filter(sl==i)
df=df_mont%>%
add_row(sl=i+0.5,first = x+2, second = y+3)
df
}
##########################################
items <- nrow(dat1)
batches <- detectedCores * 1
batchSets <- split(dat1, rep(1:batches, length.out=items))
df=lapply(batchSets, fun_2)
f=do.call("rbind", df)
f1=f[order(f$sl),]
dim(f1)