On Mon, 9 Jun 2003, Sundar Dorai-Raj wrote:
> Yes, but very slow for this example when the data.frame gets large
Indeed. However, it works when the function is not already vectorised, and
if the function is already vectorised it is unnecessary.
-thomas
_
Yes, but very slow for this example when the data.frame gets large
R> DF <- data.frame(x = rnorm(4), y = rnorm(4))
R> foo <- function(z, x, y) z[x] + z[y]
R> bar <- function(x, y) x + y
R> system.time(x <- apply(DF, 1, foo, x = "x", y = "y"))
[1] 6.94 0.04 7.37 NA NA
R> system.time
Another another neat way is:
>DF <- data.frame(x=1:4, y=rep(1,4))
> foo <- function(z,x,y)z[x]+z[y]
> apply(DF,1,foo,x="x",y="y")
1 2 3 4
2 3 4 5
or
> DF <- data.frame(x=1:4, y=rep(1,4))
> foo <- function(z)z<-x+y
> foo(DF)
[1] 2 3 4 5
"Liaw, Andy" <[EMAIL PROTECTED]> wrote:
Another neat