Search code examples
rdata.tablecross-join

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.


Solution

  • 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))