I'm studying lists and I came to an example where> L$name [1] "Fred" $wife [1] "Mary" $no.children [1] 4 $child.ages [1] 4 7 9 then following the instructions to extend the list with a new component, I executed:> L[5] <-list(NewName="something")and the new list I got was:> L$name [1] "Fred" $wife [1] "Mary" $no.children [1] 4 $child.ages [1] 4 7 9 [[5]] [1] "something" Here the 5th element lacks the name "NewName" ... maybe this is a bug. Do you have any comments? Thanks, Sergio.
Hi, On Thu, Nov 10, 2011 at 1:55 PM, JulioSergio <juliosergio at gmail.com> wrote:> I'm studying lists and I came to an example where > >> L > $name > [1] "Fred" > > $wife > [1] "Mary" > > $no.children > [1] 4 > > $child.ages > [1] 4 7 9 > > then following the instructions to extend the list with a new component, I > executed: > >> L[5] <-list(NewName="something")You've assigned a list to the 5th element of L, not the character vector you seem to be expecting. You probably want: L["NewName"] <- "something" or one of the many potential variants. Sarah> and the new list I got was: > >> L > $name > [1] "Fred" > > $wife > [1] "Mary" > > $no.children > [1] 4 > > $child.ages > [1] 4 7 9 > > [[5]] > [1] "something" > > Here the 5th element lacks the name "NewName" ... maybe this is a bug. Do you > have any comments? > > Thanks, > > Sergio. >-- Sarah Goslee http://www.functionaldiversity.org
Sarah Goslee <sarah.goslee <at> gmail.com> writes:> > Hi, >> You probably want: > L["NewName"] <- "something" > or one of the many potential variants. >Thanks, Sara, That works! However, following the idea that the syntactic notation L[i] means a sublist of L, again the syntactic notation L[i] <- list(Name=Value) should mean that the sublist is now assigned the list with the a unique named element. So I insist this is either a bug or an inconsistency of the language. Thanks again, Sergio.
Inline below ... --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. JulioSergio <juliosergio at gmail.com> wrote:>Bert Gunter <gunter.berton <at> gene.com> writes: > >> >> Inline Below. > >Well, Bert, then the manual where I found the example must be wrong. It >is: > >http://cran.r-project.org/doc/manuals/R-intro.html#Constructing-and-modifying- >lists > >And textually it says: > >Lists, like any subscripted object, can be extended by specifying >additional >components. For example > > > Lst[5] <- list(matrix=Mat)It may be true that the example is susceptible to misinterpretation, but that is certainly what you are doing. If there were four items in Lst, then after this statement there will be five, which means the list has been extended. The item that has been added is an unnamed list. The arguments to any object creation function always describe the contents of that object, not the name by which you refer to the object. If you wanted the next item in Lst to be a named list, you would refer to it by name when you assigned it: Lst["mymatrix"] <- list (matrix=Mat) But if you wanted to merge the elements of two lists you would concatenate: Lst <- c(Lst, list(matrix = Mat))
Jeff Newmiller <jdnewmil <at> dcn.davis.ca.us> writes:> > >Well, Bert, then the manual where I found the example must be wrong. It > >is: > > > >http://cran.r-project.org/doc/manuals/R-intro.html#Constructing-and-modifying-> >lists > > > >And textually it says: > > > >Lists, like any subscripted object, can be extended by specifying > >additional > >components. For example > > > > > Lst[5] <- list(matrix=Mat) > > It may be true that the example is susceptible to misinterpretation, but thatis certainly what you are doing.> > If there were four items in Lst, then after this statement there will be five,which means the list has been extended.> > The item that has been added is an unnamed list. The arguments to any objectcreation function always> describe the contents of that object, not the name by which you refer to theobject.> > If you wanted the next item in Lst to be a named list, you would refer to itby name when you assigned it:> > Lst["mymatrix"] <- list (matrix=Mat) > > But if you wanted to merge the elements of two lists you would concatenate: > > Lst <- c(Lst, list(matrix = Mat)) > >Yeah, all that you say is right, but my point is, then what is the use of puting this example in the manual (tutorial), because the same result could be obtained by simply writing:> Lst[5] <- Matistead of > Lst[5] <- list(matrix=Mat) ?
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of JulioSergio > Sent: Thursday, November 10, 2011 2:59 PM > To: r-help at stat.math.ethz.ch > Subject: Re: [R] Named components in a list > > Jeff Newmiller <jdnewmil <at> dcn.davis.ca.us> writes: > > > > > >Well, Bert, then the manual where I found the example must be wrong. It > > >is: > > > > > >http://cran.r-project.org/doc/manuals/R-intro.html#Constructing-and- > modifying- > > >lists > > > > > >And textually it says: > > > > > >Lists, like any subscripted object, can be extended by specifying > > >additional > > >components. For example > > > > > > > Lst[5] <- list(matrix=Mat) > > > > It may be true that the example is susceptible to misinterpretation, but that > is certainly what you are doing. > > > > If there were four items in Lst, then after this statement there will be five, > which means the list has been extended. > > > > The item that has been added is an unnamed list. The arguments to any object > creation function always > > describe the contents of that object, not the name by which you refer to the > object. > > > > If you wanted the next item in Lst to be a named list, you would refer to it > by name when you assigned it: > > > > Lst["mymatrix"] <- list (matrix=Mat) > > > > But if you wanted to merge the elements of two lists you would concatenate: > > > > Lst <- c(Lst, list(matrix = Mat)) > > > > > > Yeah, all that you say is right, but my point is, then what is the use of > puting this example in the manual (tutorial), because the same result could > be obtained by simply writing: > > > Lst[5] <- MatThat version is wrong. It causes a warning and results in the same thing as Lst[5] <- list(Mat[1]) You need either Lst[5] <- list(Mat) or Lst[[5]] <- Mat to make the 5th component of Lst the matrix Mat. If you want the component to named "matrix" then use one of Lst["matrix"] <- list(Mat) or Lst[["matrix"]] <- Mat Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > istead of > Lst[5] <- list(matrix=Mat) ? > > ______________________________________________ > 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.