[R] NA and logical indexes

Sebastian P. Luque spluque at gmail.com
Fri Nov 28 23:35:13 CET 2008

On Fri, 28 Nov 2008 22:01:15 -0000 (GMT),
(Ted Harding) <Ted.Harding at manchester.ac.uk> wrote:


> It is not!

Oops, of course, I meant to say "return" rather than "set" :-/


> NA can seem to have a bewildering logic, but it all becomes clear if
> you interpret NA as "value unkown".

> You asked for foo[foo$A == "b", ]. What happens is that when the test
> foo$A == "b" encounters f$A[1] it sees NA, so it does not know what
> the value is. Hence it does not know whether this row of foo satisfies
> the test. Hence the entire row is of unkown status. Hence a row is
> output all of whose elements (including the row label, i.e. the row
> number) are flagged "unknown", i.e. NA.

> AFter all, if it gave the value of foo$X[1] = 0.5013591, and you
> subsequently acessed foo[foo$A == "b",][1,2] and got 0.5013591, you
> would presumably proceed as though this was a value corresponding to a
> case where foo$A == "b". But it is not -- since foo$A[1] = NA, you
> don't know whether that is the case. Hence you don't know the value of
> foo[foo$A == "b",][1,2].

> Clear? ( :)) Hoping this helps, Ted.

Crystal!  It's something that one needs to be extra careful with when
playing with logicals.

Thanks Ted,


More information about the R-help mailing list