Sunday, 8 September 2013

Using apply and multi argument functions

Using apply and multi argument functions

I want to apply a function over a data frame. The function takes V1 as
arg1 and V2 as arg2 and I want to write the result to V3 or some other
vector.
Is there an easy and compact way to do this? I've posted a (non-working)
example below.
Thanks
Stu
my.func <- function(X, Y) {
return(X + Y)
}
a <- c(1,2,3)
b <- c(4,5,6)
my.df <- cbind(a, b)
apply(my.df, 1, my.func, X="a", Y="b")

No comments:

Post a Comment