Home > Software design >  How to use grepl function to subset rows based on multiple conditions across columns in R
How to use grepl function to subset rows based on multiple conditions across columns in R

Time:02-13

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

CodePudding user response:

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))

CodePudding user response:

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
  • Related