How to remove rows with any zero value

前端 未结 8 678
时光取名叫无心
时光取名叫无心 2020-11-28 07:04

I have a problem to solve how to remove rows with a Zero value in R. In others hand, I can use na.omit() to delete all the NA values or use complete.cases

8条回答
  •  醉酒成梦
    2020-11-28 07:28

    Using tidyverse/dplyr, you can also remove rows with any zero value in a subset of variables:

    # variables starting with Mac must be non-zero
    filter_at(df, vars(starts_with("Mac")), all_vars((.) != 0))
    
    # variables x, y, and z must be non-zero
    filter_at(df, vars(x, y, z), all_vars((.) != 0))
    
    # all numeric variables must be non-zero
    filter_if(df, is.numeric, all_vars((.) != 0))
    

提交回复
热议问题