Replace NA with 0 in a data frame column

181,837

Solution 1

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

Solution 2

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

Share:
181,837

Related videos on Youtube

Kunal Batra
Author by

Kunal Batra

Updated on July 09, 2022

Comments

  • Kunal Batra
    Kunal Batra almost 2 years

    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?

    • A5C1D2H2I1M1N2O1R2T1
      A5C1D2H2I1M1N2O1R2T1 over 11 years
      Do you want to replace NA with 0 in all columns, or just column x?
    • Kunal Batra
      Kunal Batra over 11 years
      @mrdwab: just column x
  • Kunal Batra
    Kunal Batra over 11 years
    this thing worked.thanks a lot. Could you please suggest if I did sth wrong in the above methods I tried.
  • 3pitt
    3pitt over 6 years
    isn't your 'alternatively' solution much better just for its simplicity? Does the use of two double brackets offer any advantage?
  • A5C1D2H2I1M1N2O1R2T1
    A5C1D2H2I1M1N2O1R2T1 over 6 years
    @MikePalmice, sure. The "alternatively" solution is going to be problematic if you're trying to approach the problem programmatically (for example, within a function).
  • shantanu pathak
    shantanu pathak about 4 years
    excellent explaination ..