Andres Mora
Andres Mora

Reputation: 1117

How to subset rows based on specific values from all columns

A sample dataset

v1 v2 v3 v4
1 CC DD EE
2 CC PP RR
3 EE QQ LL
4 OO RR EE
5 UU EE DD

I need to retain only rows that columns have DD and EE

v1 v2 v3 v4
1 CC DD EE
5 UU EE DD

Im trying with df %>% filter_all(any_vars(. %in% c('DD', 'EE', 'bla bla'))) but this is not considering the AND condition

   v1 v2 v3 v4
    1 CC DD EE
    5 UU EE DD

Upvotes: 1

Views: 81

Answers (3)

akrun
akrun

Reputation: 887951

Using if_any

library(dplyr)
df %>%
   filter(if_any(v2:v4, ~  . == 'DD') & if_any(v2:v4, ~ . == 'EE'))
#  v1 v2 v3 v4
#1  1 CC DD EE
#2  5 UU EE DD

Or if there are many values, then use map/reduce

library(purrr)
df %>% 
  filter(map(c("DD", "EE"), function(x) if_any(v2:v4, ~ . == x)) %>% 
           reduce(`&`))
#   v1 v2 v3 v4
# 1  1 CC DD EE
#2  5 UU EE DD

Or paste the columns and then use str_detect

library(stringr)
df %>% 
     filter(str_detect(str_c(v2, v3, v4, sep=""), "DD.*EE|EE.*DD"))
#  v1 v2 v3 v4
#1  1 CC DD EE
#2  5 UU EE DD

Upvotes: 1

jared_mamrot
jared_mamrot

Reputation: 26225

I guess this would work but it's not very concise:

library(tidyverse)
data <- tibble::tribble(~v1, ~v2, ~v3, ~v4,
                1, "CC", "DD", "EE",
                2, "CC", "PP", "RR",
                3, "EE", "QQ", "LL",
                4, "OO", "RR", "EE",
                5, "UU", "EE", "DD")
data %>% 
  filter(v2 %in% c("DD", "EE") & v3 %in% c("DD", "EE") |
         v3 %in% c("DD", "EE") & v4 %in% c("DD", "EE") |
         v2 %in% c("DD", "EE") & v4 %in% c("DD", "EE"))

Upvotes: 2

ThomasIsCoding
ThomasIsCoding

Reputation: 102890

Here is a base R option using rowSums

> df[rowSums(sapply(c("DD", "EE"), function(x) rowSums(df == x) > 0)) > 1, ]
  v1 v2 v3 v4
1  1 CC DD EE
5  5 UU EE DD

Data

> dput(df)
structure(list(v1 = 1:5, v2 = c("CC", "CC", "EE", "OO", "UU"), 
    v3 = c("DD", "PP", "QQ", "RR", "EE"), v4 = c("EE", "RR",
    "LL", "EE", "DD")), class = "data.frame", row.names = c(NA,
-5L))

Upvotes: 2

Related Questions