How do I replace NA values with zeros in an R dataframe?

后端 未结 20 1945
说谎
说谎 2020-11-21 23:41

I have a data frame and some columns have NA values.

How do I replace these NA values with zeroes?

20条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:06

    This simple function extracted from Datacamp could help:

    replace_missings <- function(x, replacement) {
      is_miss <- is.na(x)
      x[is_miss] <- replacement
    
      message(sum(is_miss), " missings replaced by the value ", replacement)
      x
    }
    

    Then

    replace_missings(df, replacement = 0)
    

提交回复
热议问题