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

Changes to a function call

I have multiple objects and I need to apply some function to them, in my example mean. But the function call shouldn’t include list, it must look like this: my_function(a, b, c).
Advise how to do it please, probably I need quote or substitute, but I’m not sure how to use them.

a <- c(1:15)
b <- c(1:17)
c <- c(1:19)
my_function <- function(objects) {
  lapply(objects, mean)
}
my_function(list(a, b, c))

>Solution :

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

A possible solution:

a <- c(1:15)
b <- c(1:17)
c <- c(1:19)

my_function <- function(...) {
  lapply(list(...), mean)
}

my_function(a, b, c)

#> [[1]]
#> [1] 8
#> 
#> [[2]]
#> [1] 9
#> 
#> [[3]]
#> [1] 10
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