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

Duncan Murdoch murdoch at stats.uwo.ca
Tue Feb 3 01:26:41 CET 2009


On 02/02/2009 6: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.
> 
> 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).

They both apply to vectors.  [] gives you a subset of the vector, [[]] 
gives you an element.  (For most vectors an element is returned as a 
subset of length 1, but for a list object, the element is different from 
a list containing it.)

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

You forgot to ask:  which manual describes this?  (The Intro to R, the R 
Language Definition both do.)

You could also try help("[[") for some useful info.

Duncan Murdoch

> Thanks for any help.
> David




More information about the R-help mailing list