lapply over a list having multiple observations of multiple variable

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)


See the FAQ: How to do a minimal reproducible example reprex for beginners

It's really hard to diagnose most of these types of question by eye.

I think this is the issue we discussed before ...

try this
run your code up until just before

df=lapply(batchSets, fun_2)

put the following to the console

debugonce(fun_2)

then run

df=lapply(batchSets, fun_2)

now you can step into the first fun_2 call.
you will see i=z$sl evaluates to a vector with multiple values
this can not be tested for with filter(sl==i) etc.

so you should rethink your function contents.

1 Like

I changed it with

batchSets <- split(dat1,  seq(items))

and works fine.

@nirgrahamuk Could you suggest how can I approach with foreach here?

Thanks

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.