Dear Madam/Sir, I am trying to understand R and I have come to a stumbling block. i have written:>Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,+child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2, +child.ages=c(14,17)))>Empl[c(2,4)]$family$spouse[1] "Fred">#instead of [1] "Fred" "Mary"Where am I wrong? Thank you very much for your patience Yours truly, Fernando Mansito [[alternative HTML version deleted]]
You need to break down your expression into parts and see what the data is:> Empl[c(2,4)]$family $family$spouse [1] "Fred" $family$children [1] 3 $family$child.ages [1] 4 7 9 $family $family$spouse [1] "Mary" $family$children [1] 2 $family$child.ages [1] 14 17> Empl[c(2,4)]$family #>> notice only picks the first object$spouse [1] "Fred" $children [1] 3 $child.ages [1] 4 7 9 To get multiple objects, then you need to use some of the 'apply' functions; in this case 'sapply': > sapply(Empl[c(2,4)], '[[', 'spouse') family family "Fred" "Mary" Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Sun, Oct 4, 2015 at 2:31 PM, FERNANDO MANSITO CABALLERO < fernando.mansito at gmail.com> wrote:> Dear Madam/Sir, > > I am trying to understand R and I have come to a stumbling block. i > have written: > > >Empl <- list(employee="Anna",family=list(spouse="Fred",children=3, > +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2, > +child.ages=c(14,17))) > >Empl[c(2,4)]$family$spouse > [1] "Fred" > >#instead of [1] "Fred" "Mary" > > Where am I wrong? > > Thank you very much for your patience > Yours truly, > Fernando Mansito > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
On Oct 4, 2015, at 11:31 AM, FERNANDO MANSITO CABALLERO wrote:> Dear Madam/Sir, > > I am trying to understand R and I have come to a stumbling block. i > have written: > >> Empl <- list(employee="Anna",family=list(spouse="Fred",children=3, > +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2, > +child.ages=c(14,17))) >> $family$spouse > [1] "Fred" >> #instead of [1] "Fred" "Mary" > > Where am I wrong?The $ function is short-hand for "[[" (with an unevaluated argument). The "[[" function is not able to deliver multiple values. You might think you needed to use: sapply( Empl[c(2,4)], function(x){ x$family$spouse ) And you cannot use that construction or its equivalent, because sapply and lapply do not pass the names of their arguments:> sapply( Empl[c(2,4)], function(x){ x[['family']]['spouse']} )$family NULL $family NULL #----------- This succeeds:> sapply( Empl[grepl('family', names(Empl)) ], function(x){x$spouse})family family "Fred" "Mary" -- David Winsemius Alameda, CA, USA
Hi Thanks a lot to all of you for your solutions and explanations. On Sun, Oct 4, 2015 at 10:34 PM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Oct 4, 2015, at 11:31 AM, FERNANDO MANSITO CABALLERO wrote: > > > Dear Madam/Sir, > > > > I am trying to understand R and I have come to a stumbling block. i > > have written: > > > >> Empl <- list(employee="Anna",family=list(spouse="Fred",children=3, > > > +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2, > > +child.ages=c(14,17))) > >> $family$spouse > > [1] "Fred" > >> #instead of [1] "Fred" "Mary" > > > > Where am I wrong? > > The $ function is short-hand for "[[" (with an unevaluated argument). The > "[[" function is not able to deliver multiple values. You might think you > needed to use: > > sapply( Empl[c(2,4)], function(x){ x$family$spouse ) > > > And you cannot use that construction or its equivalent, because sapply and > lapply do not pass the names of their arguments: > > > sapply( Empl[c(2,4)], function(x){ x[['family']]['spouse']} ) > $family > NULL > > $family > NULL > > #----------- > > > This succeeds: > > > sapply( Empl[grepl('family', names(Empl)) ], function(x){x$spouse}) > family family > "Fred" "Mary" > > > -- > > David Winsemius > Alameda, CA, USA > >[[alternative HTML version deleted]]