What is the equivalent of SQL's IN keyword in R?

user3422637 picture user3422637 · Jul 17, 2014 · Viewed 9.3k times · Source

In SQL, you can easily avoid multiple OR conditions if you're looking for many values of a particular variable (column) by using IN. For example :

SELECT * FROM colors WHERE color in ('Red', 'Blue', 'Green')

How would I do that in R? I am currently having to do it like this:

shortlisted_colors <- subset(colors, color == 'Red' | color == 'Blue' | color == 'Green')

What is a better way?

Answer

nrussell picture nrussell · Jul 17, 2014
shortlisted_colors <- subset(colors, color %in% c('Red', 'Blue', 'Green'))