[R] Named components in a list

William Dunlap wdunlap at tibco.com
Fri Nov 11 00:44:04 CET 2011


> -----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] <- Mat

That 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.



More information about the R-help mailing list