Home > Mobile >  Apply a function to a list of object and corresponding strings
Apply a function to a list of object and corresponding strings

Time:06-26

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

CodePudding user response:

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"
  •  Tags:  
  • r
  • Related