Passing arguments to iterated function through apply

后端 未结 3 690
深忆病人
深忆病人 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: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=""))
    

提交回复
热议问题