I have the following dataframe:
df <- structure(list(rows = c(1, 2, 3, 4, 5, 6), col1 = c(122, 111,
111, 222, 212, 122), col2 = c(10101, 20202, 200022, 10201, 20022,
22222), col3 = c(11, 22, 22, 22, 11, 22)), class = "data.frame", row.names = c(NA,
-6L))
rows col1 col2 col3
1 1 122 10101 11
2 2 111 20202 22
3 3 111 200022 22
4 4 222 10201 22
5 5 212 20022 11
6 6 122 22222 22
I would like to filter the rows where at least one of the columns 2,3,4 include "1" AND "2".
The desired outcome would be:
rows col1 col2 col3 1 1 122 10101 11 4 4 222 10201 22 5 5 212 20022 11 6 6 122 22222 22
The following two are not working because they scan all the three columns together and not one by one.
df[which(apply(df[,2:4],1,function(x) any(grepl("1",x)) & any(grepl("2",x)))),]
OR
library(tidyverse)
TRIPS2_fin %>% filter_at(vars(2,3,4), any_vars(str_detect(., pattern="1|2")))