2.4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRR: Select Rows Where Value Appears in Any Column

R: Select Rows Where Value Appears in Any Column

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 basic syntax to find the rows of a data frame in R in which a certain value appears in any of the columns:

library(dplyr)

df %>% filter_all(any_vars(. %in% c('value1', 'value2', ...)))

The following examples show how to use this syntax in practice.

Example 1: Find Value in Any Column

Suppose we have the following data frame in R:

#define data frame
df = data.frame(points=c(25, 12, 15, 14, 19),
                assists=c(5, 7, 7, 9, 12),
                rebounds=c(11, 8, 10, 6, 6))

#view data frame
df

  points assists rebounds
1     25       5       11
2     12       7        8
3     15       7       10
4     14       9        6
5     19      12        6

The following syntax shows how to select all rows of the data frame that contain the value 25 in any of the columns:

library(dplyr)

#select rows where 25 appears in any column
df %>% filter_all(any_vars(. %in% c(25)))

  points assists rebounds
1     25       5       11

There is exactly one row where the value 25 appears in any column.

The following syntax shows how to select all rows of the data frame that contain the values 25, 9, or 6 in any of the columns:

library(dplyr)

#select rows where 25, 9, or 6 appears in any column
df %>% filter_all(any_vars(. %in% c(25, 9, 6)))

  points assists rebounds
1     25       5       11
2     14       9        6
3     19      12        6

Example 2: Find Character in Any Column

Suppose we have the following data frame in R:

#define data frame
df = data.frame(points=c(25, 12, 15, 14, 19),
                assists=c(5, 7, 7, 9, 12),
                position=c('G', 'G', 'F', 'F', 'C'))

#view data frame
df

  points assists position
1     25       5        G
2     12       7        G
3     15       7        F
4     14       9        F
5     19      12        C

The following syntax shows how to select all rows of the data frame that contain the character G in any of the columns:

library(dplyr)

df %>% filter_all(any_vars(. %in% c('G')))

  points assists position
1     25       5        G
2     12       7        G

There are two rows where the character ‘G’ appears in any column.

The following syntax shows how to select all rows of the data frame that contain the values G or C in any of the columns:

library(dplyr)

df %>% filter_all(any_vars(. %in% c('G', 'C')))

  points assists position
1     25       5        G
2     12       7        G
3     19      12        C

Additional Resources

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

How to Replace Values in Data Frame in R
How to Sum Columns Based on a Condition in R
How to Remove Rows from Data Frame in R Based on Condition

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