0

I have an (embarrassingly) simple question: how to filter or subset a dataframe on a single value in any column of the dataframe:

df <- data.frame(
  v1 = c(1:10),
  v2 = c(10:1),
  v3 = c(1,3,2,9,5,6,1,2,3,9)
)

Say, I want to subset dfon those rows that, in any column, contain the value 9. I can do this like this:

df[df[,1]==9|df[,2]==9|df[,3]==9,]
   v1 v2 v3
2   2  9  3
4   4  7  9
9   9  2  3
10 10  1  9

But such a convoluted code for such a trivial task! There must be more efficient ways, either in base Ror other packages.

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34

2 Answers2

2

Use rowSums :

subset(df, rowSums(df == 9, na.rm = TRUE) > 0)

#   v1 v2 v3
#2   2  9  3
#4   4  7  9
#9   9  2  3
#10 10  1  9
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

additional solution:

df <- data.frame(
  v1 = c(1:10),
  v2 = c(10:1),
  v3 = c(1,3,2,9,5,6,1,2,3,9)
)

df[apply(df, 1, function(x) any(x == 9)), ]
#>    v1 v2 v3
#> 2   2  9  3
#> 4   4  7  9
#> 9   9  2  3
#> 10 10  1  9

Created on 2021-02-25 by the reprex package (v1.0.0)

using tidyverse

library(tidyverse)
df %>% 
  rowwise() %>% 
  filter(any(c_across(everything()) == 9))
#> # A tibble: 4 x 3
#> # Rowwise: 
#>      v1    v2    v3
#>   <int> <int> <dbl>
#> 1     2     9     3
#> 2     4     7     9
#> 3     9     2     3
#> 4    10     1     9

Created on 2021-02-25 by the reprex package (v1.0.0)

Yuriy Saraykin
  • 8,390
  • 1
  • 7
  • 14