Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a list of columns to data.table’s CJ as a vector

I have the following code:

main_cols <- c('num', 'let')
dt <- data.table(num = 1:5, let = letters[1:5])
dt

new_dt <- dt[CJ(num = num
                , let = let
                , unique = TRUE)
             , on = main_cols
             ]
head(new_dt, 10)

The thing is: I want to pass the columns to cross-join on as a vector. How do I “unpack” main_cols inside the CJ function? Thanks.

like image 824
Anarcho-Chossid Avatar asked Oct 27 '25 16:10

Anarcho-Chossid


1 Answers

I think you'll want to use do.call, as @AnandaMahto suggested:

m = dt[, do.call(CJ, .SD), .SDcols=main_cols]
dt[m, on=main_cols]

You could also create m this way:

m = do.call(CJ, dt[,main_cols,with=FALSE])

If you have repeating values in the columns, use the unique option to CJ:

m = dt[, do.call(CJ, c(.SD, unique=TRUE)), .SDcols=main_cols]
# or 
m = do.call(CJ, c(dt[,main_cols,with=FALSE], unique=TRUE))
like image 71
Frank Avatar answered Oct 29 '25 05:10

Frank



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!