Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments to iterated function through apply

Tags:

r

I have a function like this dummy-one:

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

Now, I would like to use the function on a dataset using apply. The problem is, that apply(data,1,FUN(parameter=1))

wont work, as FUN doesn't know what "x" is. Is there a way to tell apply to call FUN with "x" as the current row/col? `

like image 928
Produnis Avatar asked Jan 21 '11 15:01

Produnis


People also ask

What does apply () do in R?

The apply() function lets us apply a function to the rows or columns of a matrix or data frame. This function takes matrix or data frame as an argument along with function and whether it has to be applied by row or column and returns the result in the form of a vector or array or list of values obtained.

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

How do you pass an argument to a function in Python?

Information can be passed into functions as arguments. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

How do you pass a dynamic argument in Python?

Passing keyword arguments with **kwargs As you expect it, Python has also its own way of passing variable-length keyword arguments (or named arguments): this is achieved by using the **kwargs symbol. When using **kwargs, all the keywords arguments you pass to the function are packed inside a dictionary.


3 Answers

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’.

like image 199
Joshua Ulrich Avatar answered Oct 16 '22 10:10

Joshua Ulrich


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.

like image 26
Chase Avatar answered Oct 16 '22 11:10

Chase


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=""))
like image 3
Rob Avatar answered Oct 16 '22 10:10

Rob