Passing arguments to iterated function through apply

后端 未结 3 686
深忆病人
深忆病人 2020-12-13 18:18

I have a function like this dummy-one:

FUN <- function(x, parameter){
  if (parameter == 1){
      z <- DO SOMETHING WITH \"x\"}
  if (parameter ==2){
         


        
相关标签:
3条回答
  • 2020-12-13 18:50

    You want apply(data,1,FUN,parameter=1). Note the ... in the function definition:

    > args(apply)
    function (X, MARGIN, FUN, ...) 
    NULL
    

    and the corresponding entry in the documentation:

    ...: optional arguments to ‘FUN’.

    0 讨论(0)
  • 2020-12-13 18:55

    Here's a practical example of passing arguments using the ... object and *apply. It's slick, and this seemed like an easy example to explain the use. An important point to remember is when you define an argument as ... all calls to that function must have named arguments. (so R understands what you're trying to put where). For example, I could have called times <- fperform(longfunction, 10, noise = 5000) but leaving off noise = would have given me an error because it's being passed through ... My personal style is to name all of the arguments if a ... is used just to be safe.

    You can see that the argument noise is being defined in the call to fperform(FUN = longfunction, ntimes = 10, noise = 5000) but isn't being used for another 2 levels with the call to diff <- rbind(c(x, runtime(FUN, ...))) and ultimately fun <- FUN(...)

    # Made this to take up time
    longfunction <- function(noise = 2500, ...) {
      lapply(seq(noise), function(x) {
        z <- noise * runif(x)
      })
    }
    
    # Takes a function and clocks the runtime
    runtime <- function(FUN, display = TRUE, ...) {
      before <- Sys.time()
      fun <- FUN(...)
      after <- Sys.time()
      if (isTRUE(display)) {
        print(after-before)
      }
      else {
        after-before
      }
    }
    
    # Vectorizes runtime() to allow for multiple tests
    fperform <- function(FUN, ntimes = 10, ...) {   
      out <- sapply(seq(ntimes), function(x) {
        diff <- rbind(c(x, runtime(FUN, ...)))
      })
    }
    
    times <- fperform(FUN = longfunction, ntimes = 10, noise = 5000)
    
    avgtime <- mean(times[2,])
    print(paste("Average Time difference of ", avgtime, " secs", sep=""))
    
    0 讨论(0)
  • 2020-12-13 19:02

    You can make an anonymous function within the call to apply so that FUN will know what "x" is:

    apply(data, 1, function(x) FUN(x, parameter = 1))
    

    See ?apply for examples at the bottom that use this method.

    0 讨论(0)
提交回复
热议问题