[R] Extracting elements out of list in list in list
Rainer M Krug
Rainer at krugs.de
Mon Jan 19 10:29:23 CET 2015
Chel Hee Lee <chl948 at mail.usask.ca> writes:
> This approach may not be fancy as what you are looking for.
As long as it works ans=d it is efficient, it is OK.
>
>> xl <- unlist(x)
The unlist might be a problem as I am working with quite large lists.
>> xl[grep("A", names(xl))]
The grep has one problem, as it would also return fields which contain
an "A", e.g. "Alpha". I am sure this could be fixed with a regular
expression.
> f1.x1.A f1.x2.A f2.x3.A f2.x4.A
> 11 12 13 14
>>
>
> I hope this helps.
Thanks,
Rainer.
>
> Chel Hee Lee
>
> On 01/16/2015 04:40 AM, Rainer M Krug wrote:
>> Hi
>>
>> Consider the following variable:
>>
>> --8<---------------cut here---------------start------------->8---
>> x1 <- list(
>> A = 11,
>> B = 21,
>> C = 31
>> )
>>
>> x2 <- list(
>> A = 12,
>> B = 22,
>> C = 32
>> )
>>
>> x3 <- list(
>> A = 13,
>> B = 23,
>> C = 33
>> )
>>
>> x4 <- list(
>> A = 14,
>> B = 24,
>> C = 34
>> )
>>
>> y1 <- list(
>> x1 = x1,
>> x2 = x2
>> )
>>
>> y2 <- list(
>> x3 = x3,
>> x4 = x4
>> )
>>
>> x <- list(
>> f1 = y1,
>> f2 = y2
>> )
>> --8<---------------cut here---------------end--------------->8---
>>
>>
>> To extract all fields named "A" from y1, I can do
>>
>> ,----
>> | > sapply(y1, "[[", "A")
>> | x1 x2
>> | 11 12
>> `----
>>
>> But how can I do the same for x?
>>
>> I could put an sapply into an sapply, but this would be less then
>> elegant.
>>
>> Is there an easier way of doing this?
>>
>> Thanks,
>>
>> Rainer
>>
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>
--
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 494 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150119/5759f43a/attachment.bin>
More information about the R-help
mailing list