[R] better way to replace missing values with zero

From: roger bos <roger.bos_at_gmail.com>
Date: Sat 14 Jan 2006 - 05:22:05 EST

I would like to replace all missing values (NAs) with zero like below--where ever they may be--but some of the column classes are non-numeric so I get an error:

> dim(temp)

[1] 699 313
> temp[is.na(temp)] <- 0

Error in as.Date.default(value) : do not know how to convert 'value' to class "Date"

So I have to use more conveluted code:

           for (k in c(1:ncol(temp))) {
               if (class(temp[, k])=="numeric") {
                   temp[, k][is.na(temp[, k])] <- 0

This is much more code and requires a for loop. Can anyone please show me a better way?



        [[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jan 14 05:29:25 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:04 EST