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). 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()? Thanks for any help. David -- View this message in context: http://www.nabble.com/Difference-between-a--i---and-a-i--tp21801145p21801145.html Sent from the R help mailing list archive at Nabble.com.
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
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}}
I think the thing that escaped me for quite a while was tracking down the syntax to specify elements of a list element: x[[5]][3:7] to get items from within the 5th element of x. Seems IIRC there are some types of variables for which the form x$thing[3:7] fails and others for which it works fine (where the fifth element of x is named 'thing'). But I might be dreaming. Carl