Select Data Frame Rows based on Values in Vector in R (4 Examples)

 

In this tutorial, I’ll explain how to extract certain rows according to the values in a vector in the R programming language.

Table of contents:

You’re here for the answer, so let’s get straight to the R syntax…

 

Creation of Example Data

As a first step, let’s define some example data in R:

data <- data.frame(x1 = 1:10,       # Create example data
                   x2 = letters[1:10])
data                                # Return example data
#    x1 x2
# 1   1  a
# 2   2  b
# 3   3  c
# 4   4  d
# 5   5  e
# 6   6  f
# 7   7  g
# 8   8  h
# 9   9  i
# 10 10  j

Have a look at the previous RStudio console output. It shows that our example data frame has ten rows and two columns. The first variable is numeric and ranges from 1 to 10 and the second variable has the character class and ranges from the letter a to the letter j.

For this example, we also need to create a vector in R:

vec <- c(1, 7, 10)                  # Create example vector
vec                                 # Return example vector
# 1  7 10

Our vector has a length of three and consists of the numeric values 1, 7, and 10.

 

Example 1: Extract Rows Using %in%-Operator

In Example 1, I’ll show how to use the %in%-operator to extract all rows from our data frame were the values of the variable x1 are contained in our example vector.

Have a look at the following R code:

data[data$x1 %in% vec, ]            # Applying %in%-operator
#    x1 x2
# 1   1  a
# 7   7  g
# 10 10  j

Three rows of our data frame matched with the values of our vector, i.e. the filtered data frame consists of three rows.

 

Example 2: Extract Rows Using is.element Function

This Example illustrates how to use the is.element function to select specific data frame rows based on the values of a vector object. Within the is.element function, we have to specify the column of our data and the name of our vector:

data[is.element(data$x1, vec), ]    # Applying is.element function
#    x1 x2
# 1   1  a
# 7   7  g
# 10 10  j

As you can see based on the previous output of the RStudio console, the result of Examples 1 and 2 is exactly the same.

 

Example 3: Extract Rows Using filter Function of dplyr Package

So far, we have only used the basic installation of the R programming language. However, a very popular add-on package for data manipulation is the dplyr package.

This Example shows how to use the dplyr package to select certain rows of a data frame according to the values in a vector (or array). In case we want to use the functions of the dplyr package, we first have to install and load dplyr:

install.packages("dplyr")           # Install & load dplyr
library("dplyr")

Now, we can apply the filter function of the dplyr package as shown below:

filter(data, x1 %in% vec)           # Applying filter function
#   x1 x2
# 1  1  a
# 2  7  g
# 3 10  j

Again, the same output!

 

Example 4: Extract Rows Using setDT Function of data.table Package

Another popular add-on package for data manipulation is the data.table package. In Example 4, I’ll explain how to use the setDT and J functions to return specific rows of our data.

Again, we have to install and load the data.table package first:

install.packages("data.table")      # Install & load data.table
library("data.table")

Now, we can use the functions of the data.table package as follows:

setDT(data, key = "x1")[J(vec)]     # Applying setDT function
#    x1 x2
# 1:  1  a
# 2:  7  g
# 3: 10  j

The output is the same. However, the data.table package illustrates the returned data table with : in front of the data cells.

 

Video, Further Resources & Summary

Do you need more info on the R programming code of the present article? Then I can recommend to watch the following video of the Statistics Globe YouTube channel. I’m explaining the examples of this article in the video instruction:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, I can recommend to read the related tutorials of my website. Some articles are shown here:

 

To summarize: In this R tutorial you learned how to select specific rows of a data frame using a vector. In case you have any additional questions, please let me know in the comments section below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top