How to apply a custom function with several variables to each row of a data frame in R?

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 special scalar function that expects scalar inputs "x" and "y" and produces scalar output, for example

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

How to apply myfunction to every line of mydata with x being foo and y being bar?

Yes, I know that this particular example is ridiculously simple and can be very easily done in R, but I'm interested in the template. Imagine myfunction is very complex, and myfunction variable names should map to mydata column names. What is the general solution?

+4
source share
3 answers
 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
source

You can use mapply

 mapply(myfunction, mydata$foo, mydata$bar) 
+6
source

Vectorize is syntax sugar for mapply designed for this situation. This is very useful for vectorizing complex code to feed into R functions that expect it, such as outer , integrate , uniroot , etc.

 myfunction <- Vectorize(myfunction) myfunction(mydata$foo, mydata$bar) 
+1
source

Source: https://habr.com/ru/post/1489004/


All Articles