[R] fast subsetting of lists in lists

Gabor Grothendieck ggrothendieck at gmail.com
Tue Dec 7 18:55:37 CET 2010


On Tue, Dec 7, 2010 at 12:12 PM, Alexander Senger
<senger at physik.hu-berlin.de> wrote:
> Hello Gerrit, Gabor,
>
>
> thank you for your suggestion.
>
> Unfortunately unlist seems to be rather expensive. A short test with one
> of my datasets gives 0.01s for an extraction based on my approach and
> 5.6s for unlist alone. The reason seems to be that unlist relies on
> lapply internally and does so recursively?
>
> Maybe there is still another way to go?
>

Too bad it wasn't faster but note that unlist does not rely on lapply.
  Here is the complete source of unlist and from it we see the key
portion is done internally, i.e. in C:

> unlist
function (x, recursive = TRUE, use.names = TRUE)
{
    if (.Internal(islistfactor(x, recursive))) {
        lv <- unique(.Internal(unlist(lapply(x, levels), recursive,
            FALSE)))
        nm <- if (use.names)
            names(.Internal(unlist(x, recursive, use.names)))
        res <- .Internal(unlist(lapply(x, as.character), recursive,
            FALSE))
        res <- match(res, lv)
        structure(res, levels = lv, names = nm, class = "factor")
    }
    else .Internal(unlist(x, recursive, use.names))
}
<environment: namespace:base>


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list