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?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With