[R] substitute NA values

Marc Schwartz marc_schwartz at comcast.net
Wed Mar 28 16:36:20 CEST 2007


On Wed, 2007-03-28 at 16:21 +0200, Sergio Della Franca wrote:
> Dearl R-Helpers,
> 
> 
> I have the following data set:
> 
> YEAR   PRODUCTS
> 1990     2478
> 1995     3192
> 2000     NA
> 2005     1594
> 
> 
> I wanto to replace NA values, in the PRODUCTS column, with 0.
> 
> 
> How can i obtain this?
> 
> 
> Thak you in advance.
> 
> 
> Sergio Della Franca

Several ways:

1. Using replace():

  DF$PRODUCTS <- replace(DF$PRODUCTS, is.na(DF$PRODUCTS), 0)


2. Using regular indexing:

 DF$PRODUCTS[is.na(DF$PRODUCTS)] <- 0


See ?replace and ?is.na

That being said, be very cautious about doing this. Most R functions are
designed to handle NA values in very predictable ways, but not so with 0
values.  See ?NA for more information.

For example:

> DF
  YEAR PRODUCTS
1 1990     2478
2 1995     3192
3 2000       NA
4 2005     1594

> mean(DF$PRODUCTS)
[1] NA

> mean(DF$PRODUCTS, na.rm = TRUE)
[1] 2421.333


Now with:

> DF
  YEAR PRODUCTS
1 1990     2478
2 1995     3192
3 2000        0
4 2005     1594


> mean(DF$PRODUCTS)
[1] 1816


HTH,

Marc Schwartz



More information about the R-help mailing list