filter conditionally on multiple columns using regex to pick colnames r

Using dplyr::if_all you could do:

library(dplyr)

iris1 <- iris %>% 
  filter(if_all(matches(".Length"), ~ .x >= 4))

str(iris1)
#> 'data.frame':    89 obs. of  5 variables:
#>  $ Sepal.Length: num  7 6.4 6.9 5.5 6.5 5.7 6.3 6.6 5.9 6 ...
#>  $ Sepal.Width : num  3.2 3.2 3.1 2.3 2.8 2.8 3.3 2.9 3 2.2 ...
#>  $ Petal.Length: num  4.7 4.5 4.9 4 4.6 4.5 4.7 4.6 4.2 4 ...
#>  $ Petal.Width : num  1.4 1.5 1.5 1.3 1.5 1.3 1.6 1.3 1.5 1 ...
#>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 2 2 2 2 2 2 2 2 2 2 ...

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top