Hi, I am very grateful to you all, because (as you well know) if you make the database flat,
> Empl1 <- list(employee="Anna",spouse="Fred",children=3, + child.ages=c(4,7,9),employee="John",spouse="Mary",children=2, + child.ages=c(14,17)) adding a record with the same status as the first ones is feasible: > Empl1 <- c(Empl1,employee="Liz",spouse="Paul",children=1,child.ages=8) and once, thanks to you all, one learns how to obtain a first sample, > unlist(Empl1[c(2,6,10)]) spouse spouse spouse "Fred" "Mary" "Paul" it would seem that one has broken the bad spell: > unlist(Empl1[c(2,6,10)][Empl1[c(1,5,9)]==c("Anna","John","Pete")]) spouse spouse "Fred" "Mary" However, one gets more correct matches but not everywhere (Anna Liz, John Liz give incorrect results, Fred and named character(0) respectively). I have read ?"[" but I have not found where my mistake is (1). (1)In particular, the help says "Recursive (list-like) objects: indexing by [ is similar to atomic vectors and selects a list of the specified element(s)." More generally, "] can select more than one element" (as opposed to [[, $). I have tested with no added packages and with all my normally loaded packages and get the same problems in R3.2.2 Windows. Fernando On Mon, Oct 5, 2015 at 5:58 PM, FERNANDO MANSITO CABALLERO < fernando.mans...@gmail.com> wrote: > Hi > > Thanks a lot to all of you for your solutions and explanations. > > On Sun, Oct 4, 2015 at 10:34 PM, David Winsemius <dwinsem...@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]] ______________________________________________ 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.