4
votes

Suppose I have a data frame with columns named "foo" and "bar"

mydata <- data.frame(foo=rnorm(100), bar=rnorm(100))

and suppose I have a custom scalar function that expects scalar inputs "x" and "y" and produces a scalar output, for example

myfunction <- function(x, y) { if (x>0) y else x }

How do I apply myfunction to every row of mydata with x being foo, and y being bar?

Yes, I know this specific example is a ridiculously simple and can be done very easily in R, but I'm interested in the pattern.Imagine that myfunction is very complex, and the variable names of myfunction have to be mapped onto the column names of mydata. What is the general solution?

3

3 Answers

6
votes
mydata <- data.frame(x=rnorm(100), y=rnorm(100))
myfunction <- function(x, y) { if (x>0) y else x }

# with plyr (requires the argument names to match)
plyr::mdply(mydata, myfunction)

# with base functions
with(mydata, mapply(myfunction, x, y))
6
votes

You can use mapply

mapply(myfunction, mydata$foo, mydata$bar)
1
votes

Vectorize is syntactic sugar for mapply designed for this situation. It's very useful for vectorizing complicated code to feed into R functions that expect it, eg outer, integrate, uniroot, etc.

myfunction <- Vectorize(myfunction)

myfunction(mydata$foo, mydata$bar)