Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 3
    I already tried this code yesterday before you post it and not worked. Because this I posted the question. But I tried know and worked perfectly. I think I was doing something wrong. Commented Nov 17, 2011 at 14:08
  • 16
    @RenatoDinhaniConceição: if you tried something already, it's helpful to share that information when you ask the question; it helps to narrow down where the problem may be. Commented Nov 17, 2011 at 19:33
  • 2
    d[is.na(d)] <- 0 does not make sense to me. It seems backwards? How does R process this statement? Commented Apr 4, 2015 at 11:03
  • 21
    @user798719 - "<-" is R's assignment operator, and can be read as: do something on the right hand side and then assign it to the location/name on the left. In this case, we aren't really "doing" anything - just making zeroes. The left side is saying: look at the d object, inside the d object (the square brackets), find all the elements that return TRUE (is.na(d) returns a logical for each element). Once they are found, replace them ("assign them") with the value 0. These leaves all of the non-NAs as they were, and only replaces the ones with missingness. Commented Jul 29, 2015 at 16:14
  • 7
    And... if you have a data frame and only want to apply the replacement to specific nurmeric vectors (leaving say... strings with NA): df[19:28][is.na(df[19:28])] <- 0 Commented Feb 9, 2017 at 18:03