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

Radford Neal radford at cs.toronto.edu
Sat Dec 18 15:12:18 CET 2010


Duncan Murdoch writes:

  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."

But this certainly does not justify the actual behaviour.  It says
that, for example, (1:3)[NA] should not be a vector of three NAs, but
rather a vector of length zero - since NONE of the indexes are TRUE.

The actual behaviour of NA in a logical index makes no sense.  It
makes sense that NA in an integer index produces an NA in the result,
since this NA might correctly express the uncertainty in the value at
this position that follows from the uncertainty in the index (and
hence produce sensible results in subsequent operations).  But NA in a
logical index should lead to a result that is of uncertain length.
However, R has no mechanism for expressing such uncertainty, so it
makes more sense that NA in a logical index should produce an error.

   Radford Neal



More information about the R-devel mailing list