Skip to content
Prev 18753 / 63461 Next

0/1 vector for indexing leads to funny behaviour (PR#8389) (maybe a documentation deficiency?)

Yes, 0/1 (numeric) are intended to be used as index vectors -- and they 
have the semantics of numeric indices, which is that 0 elements in the 
index are omitted from the result.  This can be a very useful mode of 
operation in many situations.

I was going to write "This is described in both the introduction to R, 
and in the documentation for '['", except that I checked before I wrote 
and was surprised to be unable to any discussion of zeros in indexing in 
any of the first three places I looked:

(1) help page for '[' (There is discussion of zero indices here, but 
only in the context of using matrices to index matrices, not in the 
context of ordinary vector indices).

(2) Section 2.7 "Index vectors: selecting and modifying subsets of a 
data set" in "An Introduction to R", which does say this about numeric 
indices:
     2. A vector of positive integral quantities. In
        this case the values in the index vector must
        lie in the set {1, 2, . . . , length(x)}
(This seems to commit the sin of not telling the whole truth.)

(3) Section 5.5 "Array Indexing.  Subsections of an array" (In "An 
Introduction to R")

Question for others: did I miss something obvious, or is this a 
documentation deficiency that zeros in indices are not discussed in 3 of 
some obvious first places to look?

If indeed this is a documentation deficiency, I'm happy to contribute 
documentation patch, but I await other opinions before spending any time 
on that.

-- Tony Plate
rasche at molgen.mpg.de wrote: