Erik Brole
Erik Brole

Reputation: 355

Keep rows which don't have specific number in cells

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

Upvotes: 0

Views: 35

Answers (3)

akrun
akrun

Reputation: 887088

Use if_all

library(dplyr)
df1 %>%
   filter(!if_all(starts_with('date'), ~  .x %in% c(0, 17)))

-output

  id date1 date2
1  1    13    17
2  2     9    13

Upvotes: 1

Maël
Maël

Reputation: 51974

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

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

Upvotes: 1

asd-tm
asd-tm

Reputation: 5263

Try this:

library(dplyr)
df %>% filter(!(date1 %in% c(0L, 17L))|!(date2 %in% c(0L, 17L)))

Upvotes: 1

Related Questions