[R] Re: [R] replace NA´s

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jan 13 22:29:12 CET 2003


Jason Turner <jasont at indigoindustrial.co.nz> writes:

> On Mon, Jan 13, 2003 at 05:15:02PM +0000, juan pablo perez wrote:
> > what should I do to replace in a data frame NA´s with zeroes?
> 
> I use lapply
> 
> > dd <- data.frame(a=c(1,2,NA,4),b=c(NA,2,3,4))
> > dd
>    a  b
> 1  1 NA
> 2  2  2
> 3 NA  3
> 4  4  4
> > dd2 <- data.frame(lapply(dd,function(x,...){x[is.na(x)] <- 3.14159 ; x}))

Slightly neater:

dd2 <- dd
dd2[] <- lapply(dd2,function(x) replace(x, is.na(x), 3.14159))

-- 
   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