filtering with multiple conditions on many columns using dplyr

前端 未结 7 1482
野趣味
野趣味 2021-01-02 02:05

I\'ve searched on SO trying to find a solution to no avail. So here it is. I have a data frame with many columns, some of which are numerical and should be non-negative. I w

7条回答
  •  爱一瞬间的悲伤
    2021-01-02 02:31

    This will give you a vector of your rows that are less than 0:

    desired_rows <- sapply(target_columns, function(x) which(df[,x]<0), simplify=TRUE)
    desired_rows <- as.vector(unique(unlist(desired_rows)))
    

    Then to get a df of your desired rows:

    setdiff(df, df[desired_rows,])
      id  sth1 tg1_num sth2 tg2_num others
    1  1  dave       2   ca      35    new
    2  4 leroy       0   az      25    old
    3  5 jerry       4   mi      55    old
    

提交回复
热议问题