Table of Contents
Dplyr is a popular R package that provides a set of tools for data manipulation. Among its many functions, dplyr also allows users to select specific rows from a data frame by their names. This can be achieved by using the “select” function and specifying the desired row names as arguments. This feature is particularly useful for filtering data and performing targeted analyses on specific rows within a data frame. By utilizing dplyr’s efficient and user-friendly syntax, users can easily extract and work with specific rows of data in a more organized and streamlined manner.
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 28Notice 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 24Notice that dplyr returns only the rows whose names are not in the vector we supplied to the filter() function.
Cite this article
stats writer (2024). How can I use dplyr to select specific rows from a data frame by their names?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-select-specific-rows-from-a-data-frame-by-their-names/
stats writer. "How can I use dplyr to select specific rows from a data frame by their names?”." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-select-specific-rows-from-a-data-frame-by-their-names/.
stats writer. "How can I use dplyr to select specific rows from a data frame by their names?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-select-specific-rows-from-a-data-frame-by-their-names/.
stats writer (2024) 'How can I use dplyr to select specific rows from a data frame by their names?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-dplyr-to-select-specific-rows-from-a-data-frame-by-their-names/.
[1] stats writer, "How can I use dplyr to select specific rows from a data frame by their names?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use dplyr to select specific rows from a data frame by their names?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
