Replace NA with 0 in a data frame column [duplicate]

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[$x)] <- 0

Leave a Comment