[R] Difference between a[[i]] and a[i]

Rolf Turner r.turner at auckland.ac.nz
Tue Feb 3 03:44:16 CET 2009


On 3/02/2009, at 12:45 PM, David Epstein wrote:

>
> I'm sure I've read about the difference between a[[i]] and a[i] in  
> R, but I
> cannot recall what I read. Even more disturbing is the fact that I  
> don't
> know how to search the newsgroup for this. All the different  
> combinations I
> tried were declared not to be valid search syntax.

Essentially: a is a list; a[i] is a list of length 1 whose sole entry is
the i-th entry of a; a[[i]]] is the i-th entry of a.  If you are of a
mathematical bent it may be illuminating to think of this as being  
analogous
to, say, {3} being a subset of the set {1,2,3,4,5} and 3 being an  
element
of this set.

> 1. What sort of object can the operators [] and [[]] be applied to?  
> How do
> they differ? I mean objects in standard R, not in packages that  
> provide
> conceivable overloading of these operators (if that's possible).

Essentially lists.  Note that any vector can be considered to be a list.
Note that data frames are lists.

> 2. Meta-question: how could one search for an answer to this  
> question in the
> newsgroup?

RSiteSearch("[") or RSiteSearch("[[")

> 3. Meta-question: how could one search for an answer to this in the  
> FAQ?

Doesn't seem to be there, AFAICS.

> 4. Meta-question: how could one search for an answer to this in any  
> of the
> links one is shown after typing help.start()?

Click on ``Search Engine and Keywords''; then search on ``[[''.  Then
click on ``Extract''.  This last is less than totally perspicuous. ;-)

Instead of trying to follow these links, just do

	?"[" or ?"[["

HTH.

	cheers,

		Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}




More information about the R-help mailing list