I'm looking for a way to map a function over a list, but to be able to give the function different inputs for each iteration.
a simple example would be the following:
example_func <- function(a, b, c) {
z = a + b + c
return(z)
}
ex_list <- list(5, 14, 32)
executing on the first element in the list would look like:
example_func(ex_list[[1]], 45, 43)
I know I could also:
lapply(ex_list, function(x) example_func(x, 45, 43)
but that example would use 45 and 43 for each of the iterations in lapply. How could I give it, for example, these three sets of arguments to iterate over the three elements in the list?
c(x, 45, 43)
c(x, 3, 33)
c(x, 23, 22)
Or another similar example would be write.csv(), which takes the object and then the name of the file to be written.
set.seed(123)
df <- data.frame(x = sample(1:10, 1000, replace = TRUE))
df_list <- split(df, df$x)
lapply(df_list, function(x) write.csv(x, arg1))...
how could I iterate through this list and give the specified names to feed it?
I've done something similar to this before, but it would be a for loop iterating through seq_along the list, and then also finding that index within a vector of names. Is there a better way to do this?