Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subset data.table columns independently

I'm starting with the below table dt and try to subset its column by the list keys:

library(data.table)

set.seed(123)

randomchar <- function(n, w){
  chararray <- replicate(w, sample(c(letters, LETTERS), n, replace = TRUE))
  apply(chararray, 1, paste0, collapse = "")
}

dt <- data.table(x = randomchar(1000, 3),
                 y = randomchar(1000, 3),
                 z = randomchar(1000, 3),
                 key = c("x", "y", "z"))

keys <- with(dt, list(x = sample(x, 501),
              y = sample(y, 500),
              z = sample(z, 721)))

I can get the result I want by using a loop:

desired <- copy(dt)

for(i in seq_along(keys)){
  keyname <- names(keys)[i]
  desired <- desired[get(keyname) %in% keys[[i]]]
}

desired

The question is - Is there a more data.table idiomatic way to do this subset?

I tried using CJ: dt[CJ(keys)], but it takes a very long time.

like image 440
sebastian-c Avatar asked Mar 27 '26 03:03

sebastian-c


1 Answers

What about building a mask and filter dt on this mask:

dt[Reduce(`&`, Map(function(key, col) col %in% key, keys, dt)),]
like image 160
Colonel Beauvel Avatar answered Mar 28 '26 18:03

Colonel Beauvel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!