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

Duncan Murdoch murdoch.duncan at gmail.com
Sat Dec 18 18:23:30 CET 2010


On 18/12/2010 9:12 AM, Radford Neal wrote:
> 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.
>

I agree that the behaviour is not particularly obvious, but I'm not so 
sure it should produce an error.  We should get an error when the input 
is likely to be accidental or due to a misconception and the output 
could be accepted and lead to wrong results later.  I think using an NA 
in a logical index is probably due to a misconception (e.g. thinking it 
is an NA_integer_), but the results are so weird that they are unlikely 
to pass unnoticed.

And presumably whoever chose this behaviour back in the ancient past 
thought there was some use in including NA in a logical index, and 
someone out there in the real world has made use of it.

But I wouldn't object if R version 3 gave errors for logical index 
vectors that were the wrong length or that contained NAs.

Duncan Murdoch



More information about the R-devel mailing list