Skip to content
Prev 38677 / 63424 Next

Surprising behavior of letters[c(NA, NA)]

On 17/12/2010 10:40 AM, (Ted Harding) wrote:
I don't know where that quote came from, but it is not quite relevant 
here.  The relevant quote is in the Language Definition, talking about 
indices by type of index:

"Logical. The indexing i should generally have the same length as x. If 
it is shorter, then
its elements will be recycled as discussed in Section 3.3 [Elementary 
arithmetic operations],
page 14. If it is longer, then x is conceptually extended with NAs. The 
selected values of x
are those for which i is TRUE."

The Introduction to R gets this wrong:

"A logical vector. In this case the index vector must be of the same 
length as the vector
from which elements are to be selected. Values corresponding to TRUE in 
the index vector
are selected and those corresponding to FALSE are omitted."

The "must" in that quote is too strong; the Language Definition gets it 
right.  Perhaps the behaviour described in the Intro manual would be 
less confusing:  letters[c(NA,NA)] would give an error or warning, 
something like "logical index of incorrect length".  But I suspect 
people rely on the recycling of logical vectors, so there'd be a lot of 
complaints if we made that change.

Duncan Murdoch