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

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

    For a single vector:

    x <- c(1,2,NA,4,5)
    x[is.na(x)] <- 0
    

    For a data.frame, make a function out of the above, then apply it to the columns.

    Please provide a reproducible example next time as detailed here:

    How to make a great R reproducible example?

提交回复
热议问题