Skip to content
Prev 244196 / 398506 Next

fast subsetting of lists in lists

First, subset 'test' once, e.g.

testT <- test[1:3];

and then use sapply() on that, e.g.

val <- sapply(testT, FUN=function (x) { x$a })

Then you can avoid one level of function calls, by

val <- sapply(testT, FUN="[[", "a")

Second, there is some overhead in "[[", "$" etc.  You can use
.subset2() to avoid this, e.g.

val <- sapply(testT, FUN=.subset2, "a")

Third, it may be that using sapply() to structure you results is a bit
overkill.  If you know that the 'a' element is always of the same
dimension, you can do it yourself, e.g.

val <- lapply(testT, FUN=.subset2, "a")
val <- unlist(val, use.names=FALSE)   # use.names=FALSE is much faster than TRUE

See what that does

/Henrik

On Tue, Dec 7, 2010 at 6:47 AM, Alexander Senger
<senger at physik.hu-berlin.de> wrote: