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

Jason Turner jasont at indigoindustrial.co.nz
Mon Jan 13 21:14:04 CET 2003


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}))
> dd2
        a       b
1 1.00000 3.14159
2 2.00000 2.00000
3 3.14159 3.00000
4 4.00000 4.00000

That will generate a warning message for each non-numeric column in
your data frame.  A tidier way is left as an exercise. ;)

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
64-21-343-545
jasont at indigoindustrial.co.nz




More information about the R-help mailing list