Message-ID: <Pine.SOC.4.64.1012071557590.29234@solcom.hrz.uni-giessen.de>
Date: 2010-12-07T14:59:32Z
From: Gerrit Eichner
Subject: fast subsetting of lists in lists
In-Reply-To: <4CFE4918.9030004@physik.hu-berlin.de>
Hello, Alexander,
does
utest <- unlist(test)
utest[ names( utest) == "a"]
come close to what you need?
Hth,
Gerrit
On Tue, 7 Dec 2010, Alexander Senger wrote:
> Hello,
>
>
> my data is contained in nested lists (which seems not necessarily to be
> the best approach). What I need is a fast way to get subsets from the data.
>
> An example:
>
> test <- list(list(a = 1, b = 2, c = 3), list(a = 4, b = 5, c = 6),
> list(a = 7, b = 8, c = 9))
>
> Now I would like to have all values in the named variables "a", that is
> the vector c(1, 4, 7). The best I could come up with is:
>
> val <- sapply(1:3, function (i) {test[[i]]$a})
>
> which is unfortunately not very fast. According to R-inferno this is due
> to the fact that apply and its derivates do looping in R rather than
> rely on C-subroutines as the common [-operator.
>
> Does someone now a trick to do the same as above with the faster
> built-in subsetting? Something like:
>
> test[<somesubsettingmagic>]
>
>
> Thank you for your advice
>
>
> Alex
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>