Replace all 0 values to NA

前端 未结 8 2507
庸人自扰
庸人自扰 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:16

    #Sample data
    set.seed(1)
    dat <- data.frame(x = sample(0:2, 5, TRUE), y = sample(0:2, 5, TRUE))
    #-----
      x y
    1 0 2
    2 1 2
    3 1 1
    4 2 1
    5 0 0
    
    #replace zeros with NA
    dat[dat==0] <- NA
    #-----
       x  y
    1 NA  2
    2  1  2
    3  1  1
    4  2  1
    5 NA NA
    

提交回复
热议问题