6.2 C
London
Thursday, December 19, 2024
HomeTidyverse in Rdplyr in RHow to Select Rows of Data Frame by Name Using dplyr

How to Select Rows of Data Frame by Name Using dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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) #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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories