[R] Indexing with NA as FALSE??

ted.harding@nessie.mcc.ac.uk ted.harding at nessie.mcc.ac.uk
Fri Jul 11 18:29:50 CEST 2003


Hi Folks,

Example:
t<-c(1,2,3,4,5,6,7,8,9)
u<-c(1,NA,3,NA,5,NA,7,NA,9)
t[u==5]
--> NA NA 5 NA NA

Now, if I could somehow set things so that "NA" was FALSE for
indexing, then
t[u==5]
--> 5

I know I can do it with
t[(u==5)&(!is.na(u))]
but in the situation I am dealing with this leads to massively
cumbersome, typo-prone and hard-to-read code.

Also, as an extra, it would be very useful if, for instance,
t[u==NA] --> 2 4 6 8
(I realise that working round this is less cumbersome, but even so).

What I'm really trying to work round is the "don't know" way that R
handles NA. Reasonable in the logical sence, in that in the first
example R is saying "Can't tell whether u[2], u[4], u[6], u[8]
are equal to 5 or not, so will return a result which represents
this uncertainty", and in the second "You're telling me you don't
know what value you want to match, so ... ".

Instead of that, since NA is one of the three values TRUE, FALSE, NA
of a logical, I'd like to be able to (a) treat NA as FALSE, (b) test
for a match between NA (as specified by me) and NA (as the value of
a logical variable).

Is there any non-cumbersome way to achieve this?

With thanks,
Ted




More information about the R-help mailing list