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

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

    You can use replace()

    For example:

    > x <- c(-1,0,1,0,NA,0,1,1)
    > x1 <- replace(x,5,1)
    > x1
    [1] -1  0  1  0  1  0  1  1
    
    > x1 <- replace(x,5,mean(x,na.rm=T))
    > x1
    [1] -1.00  0.00  1.00  0.00  0.29  0.00 1.00  1.00
    

提交回复
热议问题