0

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

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.