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

后端 未结 20 1991
说谎
说谎 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:18

    dplyr example:

    library(dplyr)
    
    df1 <- df1 %>%
        mutate(myCol1 = if_else(is.na(myCol1), 0, myCol1))
    

    Note: This works per selected column, if we need to do this for all column, see @reidjax's answer using mutate_each.

提交回复
热议问题