0
votes

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?
1

1 Answers

2
votes

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