Skip to content
Prev 314707 / 398502 Next

lapply (and friends) with data.frames are slow

On Sat, Jan 5, 2013 at 7:38 PM, Kevin Ushey <kevinushey at gmail.com> wrote:
Note sure it's a huge deal, but

It does seem to be an avoidable function call with something like this:

lapply1 <- function (X, FUN, ...)
{
    FUN <- match.fun(FUN)
    if (!(is.vector(X) && is.object(X) || is.data.frame(X)))
        X <- as.list(X)
    .Internal(lapply(X, FUN))
}

On a microbenchmark:

xx <- data.frame(rnorm(5e7), rexp(5e7), runif(5e7))
xx <- cbind(xx, xx, xx, xx, xx)

system.time(lapply(x, range))
system.time(lapply1(x, range))

It saves me about 50% of the time -- that's of course only using a
relatively cheap FUN argument.

Others will hopefully comment more

M