How to Select Rows of Data Frame by Name Using dplyr


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.

Additional Resources

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

How to Filter for Unique Values Using dplyr
How to Filter by Multiple Conditions Using dplyr
How to Count Number of Occurrences in Columns in R

Leave a Reply

Your email address will not be published.