Problem:

you have to 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?

Solution

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.

Tutorial

suppose you have a sales data frame object like this:

#rstats

 

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 IN the specified group”.

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

filter_code.R

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 page Tutorial (quite obvious, isnt’ it?).

Advertisement

One thought on “Code snippet: subsetting data frame in R by vector

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s