[R] list to matrix?

R. Michael Weylandt michael.weylandt at gmail.com
Wed Dec 5 09:32:41 CET 2012


On Wed, Dec 5, 2012 at 12:02 AM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
>
>
>  p <- lapply(1:1e6, function(i)c(i, log2(i)))
>
>  system.time(z1 <- t(sapply(p,function(x)x)))
> #   user  system elapsed
>  # 2.568   0.048   2.619
>  system.time(z1 <- do.call(rbind,p))
> #   user  system elapsed
>  # 4.000   0.052   4.060
> A.K.

Thanks for that Arun -- I'll have to look into why rbind is so slow.

Some interesting notes:

1) On my machine

t(sapply(p, function(x) x))

is about 2x faster than

t(sapply(p, identity))

2) Similarly,

do.call(rbind, p)

is about 4x slower than

do.call(cbind, p)

which all goes to show, profiling is all-important (as Bill reminds me
often) and often counter-intuitive.

Michael




More information about the R-help mailing list