[R] List or matrix of object

Greg Snow Greg.Snow at imail.org
Tue Oct 12 19:20:45 CEST 2010


The difference is the same as the difference between a set with 1 element and a single element from a set.

The single [ extracts/replaces/assigns a subset of the list elements, but the piece is still a list (even if it is one element).  So when you are assigning using [ you need to give  it a list, not a single object. If you create a list then do something like mode( mylist[1] ) you will see that it is still a list.

The double [[ exctracts/replaces/assigns a single element of the list, it does not work on anything more than a single element, but it works with that element as its own object, not a list (unless it is a list).  So [[ can be used to assign a single element without needing to create a list (but can only do a single element where [ can do 1 or more).  If you do mode( mylist[[1]] ) then you will see that the single element is no longer a list.

Hope that helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Filoche
> Sent: Tuesday, October 12, 2010 9:29 AM
> To: r-help at r-project.org
> Subject: Re: [R] List or matrix of object
> 
> 
> Hi again everyone.
> 
> I found I could use a list with
> 
> l = list()
> l[[1]] = myObj
> 
> instead of
> 
> l[1] = myObj
> 
> Anyone can explain me why the use of double [] is required?
> 
> Regards,
> Phil
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/List-or-
> matrix-of-object-tp2992101p2992121.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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