6

I am looking for an dplyr equivalent on

SELECT user_id, item 
  FROM users
  WHERE user_id NOT IN (1, 5, 6, 7, 11, 17, 18); -- admin accounts

I can use users %>% filter(user_id != 1) but can't imagine using multiple && all the way.

Is there a way to exclude a number of rows?

Young Ha Kim
  • 127
  • 1
  • 2
  • 7

1 Answers1

12

You can use ! and %in%:

filtered_users <- filter(users, !user_id %in% c(1, 5, 6, 7, 11, 17, 18))

This is based on https://stackoverflow.com/a/34444336/1152809. I just googled "dplyr not in" and this was the first result. Google is your friend when learning new things. Also, as @thelatemail said, %in% is a base R function.

Community
  • 1
  • 1
Travis Heeter
  • 13,002
  • 13
  • 87
  • 129