[R] Indexing with NA as FALSE??

Thomas Lumley tlumley at u.washington.edu
Fri Jul 11 19:07:29 CEST 2003


On Fri, 11 Jul 2003 ted.harding at nessie.mcc.ac.uk wrote:

> 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

t[u %in% 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

t[ u %in% NA]


	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list