How to select rows of dataframe by name using dplyr?

Using the dplyr package in R, you can select rows of a dataframe by name using the filter() function. This function takes the name of the dataframe as the first argument, followed by the argument criteria in the form of a logical expression. The result of the filter will be a subset of the original dataframe containing only the rows that satisfy the logical expression. For example, the following command will select all rows of the dataframe where the column “name” is equal to “John”: filter(dataframe, name == “John”).


You can use the following syntax to select rows of a data frame by name using dplyr:

library(dplyr)

#select rows by name
df %>%
  filter(row.names(df) %in% c('name1', 'name2', 'name3'))

The following example shows how to use this syntax in practice.

Example: Select Rows by Name Using dplyr

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#set row names
row.names(df) <- c('Mavs', 'Hawks', 'Cavs', 'Lakers', 'Heat')

#view data frame
df

       points assists rebounds
Mavs       99      33       30
Hawks      90      28       28
Cavs       86      31       24
Lakers     88      39       24
Heat       95      34       28

We can use the following code to select the rows where the row name is equal to Hawks, Cavs, or Heat:

library(dplyr)

#select specific rows by name
df %>%
  filter(row.names(df) %in% c('Hawks', 'Cavs', 'Heat'))

      points assists rebounds
Hawks     90      28       28
Cavs      86      31       24
Heat      95      34       28

Notice that dplyr returns only the rows whose names are in the vector we supplied to the filter() function.

Also note that you can use an exclamation point ( ! ) to select all rows whose names are not in a vector:

library(dplyr)

#select rows that do not have Hawks, Cavs, or Heat in the row name
df %>%
  filter(!(row.names(df) %in% c('Hawks', 'Cavs', 'Heat')))

       points assists rebounds
Mavs       99      33       30
Lakers     88      39       24

Notice that dplyr returns only the rows whose names are not in the vector we supplied to the filter() function.

The following tutorials explain how to perform other common tasks in R:

x