Replace all 0 values to NA

前端 未结 8 2595
庸人自扰
庸人自扰 2020-11-22 08:48

I have a dataframe with some numeric columns. Some row has a 0 value which should be considered as null in statistical analysis. What is the fastest way to replace all the 0

8条回答
  •  再見小時候
    2020-11-22 09:19

    An alternative way without the [<- function:

    A sample data frame dat (shamelessly copied from @Chase's answer):

    dat
    
      x y
    1 0 2
    2 1 2
    3 1 1
    4 2 1
    5 0 0
    

    Zeroes can be replaced with NA by the is.na<- function:

    is.na(dat) <- !dat
    
    
    dat
    
       x  y
    1 NA  2
    2  1  2
    3  1  1
    4  2  1
    5 NA NA
    

提交回复
热议问题