'How to use grepl function to subset rows based on multiple conditions across columns in R
I am trying to subset a dataset using the grepl function.
I want to retain rows where all columns contain '@'
I tried this code but it doesn't work.
all_nullx <- riv %>% with(riv, riv[ grepl( '@', col1) & grepl( '@', col2) & grepl('@', col3) & grepl('@', left_index) & grepl('@', right_middle) & grepl('@', col4), ])
Thanks
Solution 1:[1]
Tidyverse
We can apply a function across all columns using everything() inside of across, then keep only rows that have @ in every column.
library(tidyverse)
riv %>%
filter(across(everything(), ~ grepl("@", .)))
Or with stringr:
riv %>%
filter(across(everything(), ~ str_detect(., "@")))
base R
Or we can use grepl with Reduce from base R:
riv[Reduce(`&`, lapply(riv, grepl, pattern = "@")),]
Or one more base R possibility:
riv[apply(riv , 1 , function(x) all(grepl("@", x))), ]
Output
a b c
1 A@r C@r F@r
2 B@r D@r G@r
Data
riv <- structure(list(
a = c("A@r", "B@r", "Rr"),
b = c("C@r", "D@r", "E@r"),
c = c("F@r", "G@r", "Hr")
),
class = "data.frame",
row.names = c(NA,-3L))
Solution 2:[2]
one nice option within the tidyverse, specifically dplyr in this case, can be the if_all() function (I will use @andrews data)
riv <- structure(list(
a = c("A@r", "B@r", "Rr"),
b = c("C@r", "D@r", "E@r"),
c = c("F@r", "G@r", "Hr")
),
class = "data.frame",
row.names = c(NA,-3L))
library(dplyr)
riv %>%
dplyr::filter(if_all(everything(), ~grepl("@", .x)))
a b c
1 A@r C@r F@r
2 B@r D@r G@r
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | DPH |
