On May 26, 2015, at 3:59 PM, blue honour via R-help wrote:

> Dear R users,
> I have a question regarding retrieving data from nested lists. To illustrate, 
> here is an example input:
> d1<-data.table(v1=c(3,4),v2=c(2,5))
> d2<-data.table(v1=c(1,2),v2=c(1,1))l1<-list(d1,name="test")l2<-list(d2,name="test2")motherlist<-list()motherlist$l1<-l1motherlist$l2<-l2
> Let's say we are given motherlist as above. We would like to get the union of 
> the contents of v1 vector from d1 data.table and the v1 vector from d2 
> data.table. How can we achieve this in a compact one line command using 
> motherlist as input please? 
> Thank you. 
> 
> 
> 
>       [[alternative HTML version deleted]]

Your posting is a good example of the reason we ask people _not_ to post in 
HTNL. Linefeeds (among many other useful bits of information) get lost:

d1<-data.table(v1=c(3,4),v2=c(2,5))
d2<-data.table(v1=c(1,2),v2=c(1,1))
l1<-list(d1,name="test")
l2<-list(d2,name="test2")
motherlist<-list()
motherlist$l1<-l1
motherlist$l2<-l2



> with( motherlist, c( l1[1][[1]]$v1, l2[1][[1]]$v1) )
[1] 3 4 1 2

OR:
> with( motherlist, c( l1[[1]]$v1, l2[[1]]$v1) )
[1] 3 4 1 2

That used the inheritance of data.tables from data.frames. This is data.table 
extraction syntax:

> with( motherlist, c( l1[[1]][ ,v1], l2[[1]][ ,v1]) )
[1] 3 4 1 2

Another approach with the display of intermediate values:

> sapply(motherlist, "[", 1)
$l1
   v1 v2
1:  3  2
2:  4  5

$l2
   v1 v2
1:  1  1
2:  2  1

> sapply( sapply(motherlist, "[", 1) , "[[", 1)
     l1 l2
[1,]  3  1
[2,]  4  2

> c( sapply( sapply(motherlist, "[", 1) , "[[", 1) )
[1] 3 4 1 2

Please read the posting guide.

> 
> ______________________________________________
> R-help@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.

David Winsemius
Alameda, CA, USA

______________________________________________
R-help@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.

Reply via email to