Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply a function to a list of object and corresponding strings

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading