Home > Enterprise >  Look for a string in a data frame and select the rows with R
Look for a string in a data frame and select the rows with R

Time:07-20

I have a table like this

Column A Column B Column C Column D
A x 1 k1
B k 2 k2
C z 3 k3
D y 4 k4

I would like to write a script which selects a specific string and gives me back that row. For example, I want to see the rows which contains "A", which can be in every column. I tried str_detect, but you have to specify the column in the data frame which I don't want to. Also it would be perfect to have it selecting different strings, like look for "A", "3" and "y" with this output:

Column A Column B Column C Column D
A x 1 k1
C z 3 k3
D y 4 k4

CodePudding user response:

We could use if_any with str_detect

library(dplyr)
library(stringr)
df1 %>% 
   filter(if_any(everything(), ~ str_detect(.x, 'A|3|y')))

-output

  ColumnA ColumnB ColumnC ColumnD
1       A       x       1      k1
2       C       z       3      k3
3       D       y       4      k4

Or using base R with a non-regex solution

subset(df1, Reduce(`|`, lapply(df1, \(x) x %in% c('A', 3, 'y'))))
  ColumnA ColumnB ColumnC ColumnD
1       A       x       1      k1
3       C       z       3      k3
4       D       y       4      k4

data

df1 <- structure(list(ColumnA = c("A", "B", "C", "D"), ColumnB = c("x", 
"k", "z", "y"), ColumnC = 1:4, ColumnD = c("k1", "k2", "k3", 
"k4")), class = "data.frame", row.names = c(NA, -4L))

CodePudding user response:

vec <- c('A', 3, 'y') 
df[rowsum( (as.matrix(df) %in% vec), c(row(df)))>0,]

  ColumnA ColumnB ColumnC ColumnD
1       A       x       1      k1
3       C       z       3      k3
4       D       y       4      k4

Another way is useing regex:

df[grepl(sprintf('\\b(%s)\\b', paste0(vec, collapse = '|')), do.call(paste, df)), ]
  ColumnA ColumnB ColumnC ColumnD
1       A       x       1      k1
3       C       z       3      k3
4       D       y       4      k4
  • Related