2 min read

Code snippet: subsetting data frame in R by vector



you haveto subset a data frame using as criteria the exact match of a vector content.

for instance:

you have a dataset with some attributes, and you have a vector with some values of one of the attributes. You want to make a filter based on the values in the vector.

Example: sales records, each record is a deal.

The vector is a list of selected customers you are interested in.

Is it possible to make such a kind of filter?


Of course it is!

you just have to use the %in% operator.

let’s see how to do it in the short tutorial here below.


suppose you have a sales data frame object like this:


suppose you want to extract sales to Francesca, Tommaso and Gianna.

first, you have to assign those names to a vector.

vector = c(“Francesca","Tommaso","Gianna")

then, you can write the filtering statement, using the_%in%_operator.

query_result = sales[sales$customer %in% vector,]

and that’s it!

The meaning of _%in%_operator is exactly the one you guess:

“ select only values present INthe specified group”.

Full code is available as an R workbook for quick reference:


Let me know if you use any other method to obtain the same result.

Finally,if you enjoyed the tutorial, you can find more tutorial on pageTutorial(quite obvious, isnt’ it?).

comments disclaimer
thank you for taking the time to comment. If the comment you are about to write is related to a piece of code I wrote, please reach its Github respository and place there any request of improvement or report of bugs.