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

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

    I know the question is already answered, but doing it this way might be more useful to some:

    Define this function:

    na.zero <- function (x) {
        x[is.na(x)] <- 0
        return(x)
    }
    

    Now whenever you need to convert NA's in a vector to zero's you can do:

    na.zero(some.vector)
    

提交回复
热议问题