37

Possible Duplicate:
Set NA to 0 in R

I have a data.frame with a column having NA values. I want to replace NA with 0 or any other value. I have tried a lot of threads and methods but it did not give me the result. I have tried the below methods.

a$x[a$x == NA] <- 0;
a[ , c("x")] <- apply(a[ , c("x")], 1, function(z){replace(z, is.na(z), 0)});
a$x[is.na(a$x), ] <- 0;

None of the above methods replaced NA with 0 in column x for data.frame a. Why?

Henrik
  • 56,228
  • 12
  • 124
  • 139
Kunal Batra
  • 801
  • 3
  • 13
  • 22

2 Answers2

87

Since nobody so far felt fit to point out why what you're trying doesn't work:

  1. NA == NA doesn't return TRUE, it returns NA (since comparing to undefined values should yield an undefined result).
  2. You're trying to call apply on an atomic vector. You can't use apply to loop over the elements in a column.
  3. Your subscripts are off - you're trying to give two indices into a$x, which is just the column (an atomic vector).

I'd fix up 3. to get to a$x[is.na(a$x)] <- 0

themel
  • 8,505
  • 1
  • 28
  • 31
13

First, here's some sample data:

set.seed(1)
dat <- data.frame(one = rnorm(15),
                 two = sample(LETTERS, 15),
                 three = rnorm(15),
                 four = runif(15))
dat <- data.frame(lapply(dat, function(x) { x[sample(15, 5)] <- NA; x }))
head(dat)
#          one  two       three      four
# 1         NA    M  0.80418951 0.8921983
# 2  0.1836433    O -0.05710677        NA
# 3 -0.8356286    L  0.50360797 0.3899895
# 4         NA    E          NA        NA
# 5  0.3295078    S          NA 0.9606180
# 6 -0.8204684 <NA> -1.28459935 0.4346595

Here's our replacement:

dat[["four"]][is.na(dat[["four"]])] <- 0
head(dat)
#          one  two       three      four
# 1         NA    M  0.80418951 0.8921983
# 2  0.1836433    O -0.05710677 0.0000000
# 3 -0.8356286    L  0.50360797 0.3899895
# 4         NA    E          NA 0.0000000
# 5  0.3295078    S          NA 0.9606180
# 6 -0.8204684 <NA> -1.28459935 0.4346595

Alternatively, you can, of course, write dat$four[is.na(dat$four)] <- 0

A5C1D2H2I1M1N2O1R2T1
  • 177,446
  • 27
  • 370
  • 450
  • this thing worked.thanks a lot. Could you please suggest if I did sth wrong in the above methods I tried. – Kunal Batra Nov 01 '12 at 07:56
  • isn't your 'alternatively' solution much better just for its simplicity? Does the use of two double brackets offer any advantage? – 3pitt Oct 27 '17 at 13:42
  • 1
    @MikePalmice, sure. The "alternatively" solution is going to be problematic if you're trying to approach the problem programmatically (for example, within a function). – A5C1D2H2I1M1N2O1R2T1 Oct 27 '17 at 15:36