简体   繁体   中英

How to filter data.table using grepl with multiple patterns on multiple columns in R

I have a data.table in R like below

col1 col2  col3
A     AA    1
C     BB   2

DT <- data.table(col1=c('A','C'), col2=c('AA','BB'), col3=c(1,2))

I want to filter the data.table such that col1 is in A/B/C and col2 is in AA/CC/ . If I know the number of columns to search is fixed then I can do

DT[Vectorize(grepl)(col1, "A/B/C") & Vectorize(grepl)(col2, "AA/CC/")]
  • How can I filter if the number of columns to filter on is dynamic?
  • Is there a way to increase the speed of this filter?

You could create a vector of column names and a list of values that you are looking for in those columns and then use Map to select rows.

library(data.table)

cols <- paste0('col', 1:2)
values <- list(c('A', 'B', 'C'), c('AA', 'CC'))
DT[Reduce(`&`, Map(`%in%`, DT[, ..cols], values))]

#   col1 col2 col3
#1:    A   AA    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM