Home > database >  Calling chosen functions from the list of functions for several objects
Calling chosen functions from the list of functions for several objects

Time:06-03

I've made a function that applies several functions on several objects. I have them in the list because this all should be wrapped in one function

myFunc <- function(...) {
  myApply <- function(...){
  flist <- list(min, max, mean, sum)
  lapply(flist, function(f) f(...))
}
# apply function to several objects  
lapply(list(...), myApply)
}

obj1 <- c(1:9)
obj2 <- c(1:7)

myFunc(obj1, obj2)

My question is how to call not all functions from the list, but for example, only two of them: myFunc(obj1, obj2, list(min, max)). Also maybe someone knows how to make this construction more neat?

CodePudding user response:

What about this approach?

myFun <- function(...,fns) lapply(fns,\(i) lapply(list(...),i))

Usage:

myFun(obj1,obj2, fns=list(min, max))
  • Related