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 -- Botany Department University of Vermont 109 Carrigan Drive Burlington, VT 05405
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
See ?is.na Uwe Ligges Benjamin M. Osborne wrote:> 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 >
Have you considered "is.na"? My favorite tool for answering questions like this is www.r-project.org -> search -> "R site search". When I asked there for "subsetting by NA" just now, I got 101 hits, the third of which mentioned "is.na". Also, the posting guide "R-project.org/posting-guide.html" gives other useful tips for how to find information about R. hope this helps. spencer graves Benjamin M. Osborne wrote:>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 > > >