Replace all 0 values to NA

前端 未结 8 2594
庸人自扰
庸人自扰 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 08:58

    In case anyone arrives here via google looking for the opposite (i.e. how to replace all NAs in a data.frame with 0), the answer is

    df[is.na(df)] <- 0
    

    OR

    Using dplyr / tidyverse

    library(dplyr)
    mtcars %>% replace(is.na(.), 0)
    

提交回复
热议问题