How can I select rows from a data set where a certain value appears in any column?

How can I select rows from a data set where a certain value appears in any column?

To select rows from a data set where a certain value appears in any column, one can use a filtering or querying method. This involves specifying the desired value and the columns to search for it, then applying the appropriate function or condition to retrieve the matching rows. This technique allows for the extraction of specific data points or observations that meet the criteria, providing a more focused and targeted view of the data. By using this method, one can efficiently and effectively access the relevant information needed for analysis or decision-making.

R: Select Rows Where Value Appears in Any Column


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.

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

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

Cite this article

stats writer (2024). How can I select rows from a data set where a certain value appears in any column?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-data-set-where-a-certain-value-appears-in-any-column/

stats writer. "How can I select rows from a data set where a certain value appears in any column?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-data-set-where-a-certain-value-appears-in-any-column/.

stats writer. "How can I select rows from a data set where a certain value appears in any column?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-data-set-where-a-certain-value-appears-in-any-column/.

stats writer (2024) 'How can I select rows from a data set where a certain value appears in any column?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-rows-from-a-data-set-where-a-certain-value-appears-in-any-column/.

[1] stats writer, "How can I select rows from a data set where a certain value appears in any column?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I select rows from a data set where a certain value appears in any column?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top