[R] deleting specified NA values

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Nov 1 18:09:56 CET 2004


Gabor Grothendieck <ggrothendieck at myway.com> writes:

> BXC (Bendix Carstensen <bxc <at> steno.dk> writes:
> : 
> : > From: r-help-bounces <at> stat.math.ethz.ch 
> : > [mailto:r-help-bounces <at> stat.math.ethz.ch] On Behalf Of Robert 
> : > 
> : > I have a data set of about 10000 records which was compiled 
> : > from several smaller data sets using SPSS. During compilation 
> : > 88 false records were accidentally introduced which comprise 
> : > all NA values.  I want to delete these records but not other 
> : > missing data.  The functions na.exclude and na.omit seem to 
> : > remove all values of NA? How can I delete just the relevant 
> : > NA's?  . i.e. I want to delete  all records in the data frame 
> : > DATA where the field age contains NA values
> : 
> : How about:
> : 
> : all.nas <- apply( old, 1, function(x) sum( is.na( x ) ) )
> : new <- old[all.nas < dim( old )[2], ]
> 
> A minor simplification of this might be:
> 
> 	x[rowSums(is.na(x)) < ncol(x),]

If you want to get sneaky, there's also

 x[!!rowSums(!is.na(x)),]

although I think I'd prefer

 x[apply(!is.na(x),1,any),]

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list