Dear R users,I have a list of equally structured lists, how can I access e.g. all 2nd compontents in those sub-lists?An example:lst <- list(rep(list(1:3),3), rep(list(4:6),3))> lst[[1]][[1]][[1]][1] 1 2 3[[1]][[2]][1] 1 2 3[[1]][[3]][1] 1 2 3[[2]][[2]][[1]][1] 4 5 6[[2]][[2]][1] 4 5 6[[2]][[3]][1] 4 5 6What I want to get are all second sub-lists, in this case:[[1]][[2]][1] 1 2 3and[[2]][[2]][1] 4 5 6many thanksBjörn -- View this message in context: http://r.789695.n4.nabble.com/Access-comonents-in-lists-of-lists-tp4655224.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
lapply(lst, function(x) return(x[[2]])) On Fri, Jan 11, 2013 at 6:40 AM, Bjoern Helm <bjoern.helm at tu-dresden.de> wrote:> Dear R users,I have a list of equally structured lists, how can I access e.g. > all 2nd compontents in those sub-lists?An example:lst <- > list(rep(list(1:3),3), rep(list(4:6),3))> lst[[1]][[1]][[1]][1] 1 2 > 3[[1]][[2]][1] 1 2 3[[1]][[3]][1] 1 2 3[[2]][[2]][[1]][1] 4 5 6[[2]][[2]][1] > 4 5 6[[2]][[3]][1] 4 5 6What I want to get are all second sub-lists, in this > case:[[1]][[2]][1] 1 2 3and[[2]][[2]][1] 4 5 6many thanksBj?rn > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Access-comonents-in-lists-of-lists-tp4655224.html > Sent from the R help mailing list archive at Nabble.com. > [[alternative HTML version deleted]] > > > ______________________________________________ > 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. >
Hi, thank you. I just came up with lapply(c(1:length(lst)), function(x) return(lst[[x]][[2]])) but your solution is much more elegant. Although I don't completely understand: if lst is the argument of function x[[2]] this should be iterpreted as lst[[2]]. Where am I wrong? Björn Am 11.01.2013 15:44, schrieb arun kirshna [via R]:> Hi, > Try this: > lapply(lst,function(x) x[[2]]) > #or > lst1<-lapply(lst,function(x) {names(x)<-1:3;x}) > lapply(lst1,function(x) x$`2`) > #[[1]] > #[1] 1 2 3 > > #[[2]] > #[1] 4 5 6 > > A.K. > > ------------------------------------------------------------------------ > If you reply to this email, your message will be added to the > discussion below: > http://r.789695.n4.nabble.com/Access-comonents-in-lists-of-lists-tp4655224p4655235.html > > To unsubscribe from Access comonents in lists of lists, click here > <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4655224&code=YmpvZXJuLmhlbG1AdHUtZHJlc2Rlbi5kZXw0NjU1MjI0fC0xMTQwNTkyMzI=>. > NAML > <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> >-- Björn Helm Institut für Siedlungs- und Industriewasserwirtschaft Technische Universität Dresden D-01062 Dresden Tel: +49 (0) 351 463 34616 Fax: +49 (0) 351 463 37204 http://tu-dresden.de/hydro/siedlungswasserwirtschaft -- View this message in context: http://r.789695.n4.nabble.com/Access-comonents-in-lists-of-lists-tp4655224p4655302.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
On 01/12/2013 12:40 AM, Bjoern Helm wrote:> Dear R users,I have a list of equally structured lists, how can I access e.g. > all 2nd compontents in those sub-lists?An example:lst <- > list(rep(list(1:3),3), rep(list(4:6),3))> lst[[1]][[1]][[1]][1] 1 2 > 3[[1]][[2]][1] 1 2 3[[1]][[3]][1] 1 2 3[[2]][[2]][[1]][1] 4 5 6[[2]][[2]][1] > 4 5 6[[2]][[3]][1] 4 5 6What I want to get are all second sub-lists, in this > case:[[1]][[2]][1] 1 2 3and[[2]][[2]][1] 4 5 6Your email was/is a bit hard to read .... but if I understand correctly, what you want is lst2 <- lapply(lst,function(x){x[[2]]}) See ?lapply. Also associated functions like tapply() and sapply(). Very useful gadgets. And if you really get hooked on such functionality, you might want to have a look at the "plyr" package. cheers, Rolf Turner