Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr Exclude row [duplicate]

Tags:

r

dplyr

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?

like image 931
Young Ha Kim Avatar asked Dec 20 '16 02:12

Young Ha Kim


1 Answers

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.

like image 109
Travis Heeter Avatar answered Sep 30 '22 00:09

Travis Heeter