1

I have a list of objects object_list a list of strings string_list and a function that needs an object and a string.

I want to apply my function to every object and pass a different string from a list each time. The string_list and object_list have the same length and order.

How do I do this?

I tried:

myfuntion <- function(object,filename) {
}

input <- list(a,b,c)

filename_list <- c("a","b","c")

lapply(input, myfunction, filename=filename_list)

But that does not work

zeratoss
  • 47
  • 4
  • 1
    Please, provide a minimal reproducible example: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – PaulS Jun 25 '22 at 13:13
  • I added a longer example, I hope this is more clear – zeratoss Jun 25 '22 at 13:29

1 Answers1

1

You may try the following, using mapply:

myfuntion <- function(object,filename) {
  paste(object, filename)
}

input <- list("a","b","c")

filename_list <- c("a","b","c")

mapply(myfuntion, input, filename_list)

#> [1] "a a" "b b" "c c"
PaulS
  • 21,159
  • 2
  • 9
  • 26