Home > OS >  Keep rows which don't have specific number in cells
Keep rows which don't have specific number in cells

Time:11-09

Having a dataframe like this:

structure(list(id = c(1, 2, 3), date1 = c(13, 9, 0), date2 = c(17L, 
13L, 17L)), row.names = c(NA, -3L), class = "data.frame")

How is it possible to check if a row has different numbers than 0 or/and 17 and keep them into a new dataframe (excluding id column)?

Example expected output

  id date1 date2
  1    13    17
  2     9    13

CodePudding user response:

data[rowSums(data[-1] != c(0, 17)) != 0, ]

#  id date1 date2
#1  1    13    17
#2  2     9    13

CodePudding user response:

Try this:

library(dplyr)
df %>% filter(!(date1 %in% c(0L, 17L))|!(date2 %in% c(0L, 17L)))
  •  Tags:  
  • r
  • Related