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:
NA == NA
doesn't returnTRUE
, it returnsNA
(since comparing to undefined values should yield an undefined result).- You're trying to call
apply
on an atomic vector. You can't useapply
to loop over the elements in a column. - 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
Related videos on Youtube
Author by
Kunal Batra
Updated on July 09, 2022Comments
-
Kunal Batra almost 2 years
Possible Duplicate:
Set NA to 0 in RI have a data.frame with a column having
NA
values. I want to replaceNA
with0
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 columnx
for data.framea
. Why?-
A5C1D2H2I1M1N2O1R2T1 over 11 yearsDo you want to replace
NA
with0
in all columns, or just columnx
? -
Kunal Batra over 11 years@mrdwab: just column x
-
-
Kunal Batra over 11 yearsthis thing worked.thanks a lot. Could you please suggest if I did sth wrong in the above methods I tried.
-
3pitt over 6 yearsisn't your 'alternatively' solution much better just for its simplicity? Does the use of two double brackets offer any advantage?
-
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 about 4 yearsexcellent explaination ..