[R] subsetting by NA

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Fri Feb 25 18:03:35 CET 2005



Benjamin M. Osborne allegedly said on 2/25/2005 10:33 AM:
> I want to know where all the NAs are in a matrix.  The data frame looks like
> this:
> 
> 
>>vmc[1:5,]
> 
>        date year month day snow.new prcp      tmin snow.dep       tmax
> 1 01NOV1954 1954    11   1       NA   NA -14.44444       NA 12.2222222
> 2 02NOV1954 1954    11   2       NA   NA -13.88889       NA  2.2222222
> 3 03NOV1954 1954    11   3       NA   NA -16.66667       NA -1.1111111
> 4 04NOV1954 1954    11   4       NA   NA        NA       NA -0.5555556
> 5 05NOV1954 1954    11   5       NA   NA -17.22222       NA -2.7777778
>        tmean yearmo
> 1  -1.111111 195411
> 2  -5.833333 195411
> 3  -8.888889 195411
> 4         NA 195411
> 5 -10.000000 195411
> 
> 
> This does not work:
> 
>>subset(vmc, snow.new==NA)
> 
>  [1] date     year     month    day      snow.new prcp     tmin     snow.dep
>  [9] tmax     tmean    yearmo
> <0 rows> (or 0-length row.names)
> 
> 
> Because:
> 
>>NA==NA
> 
> [1] NA
> 
> How can I return all rows of this data frame that contain NA in a particular
> field?
> 
> Thanks,
> Ben Osborne
> 


Ben,

This is a perfect job for ?complete.cases. You should also read 
Introduction to R, pg. 9-10 in the PDF document, regarding how missing 
values are handled in R.

--sundar




More information about the R-help mailing list