CJ.dt.Rd
similar to data.table::CJ and base::expand.grid except for rows of data.tables.
CJ.dt(..., groups = NULL)
data.tables
a character vector corresponding to column names of grouping vars in all of the data.tables
CJ.dt computes successive cartesian join over rows of each table paying no attention to whatever the tables are keyed on.
#' CJ.dt(data.table(c(1,2,2),c(1,1,1)),data.table(c("a","b"),c("c","d")))
#If you want to expand x to unique values of a non-unique columns in y
x <- data.table(c(1,2,3),c("a","b","b"))
y <- data.table(id=c(1,2,2,1,3),value=c(2,4,1,7,3))
z <- CJ.dt(x, y[,list(id=unique(id))])
#if you want to merge this back to y
y[z,on="id",allow.cartesian=TRUE] #or z[y,on="id",allow.cartesian=TRUE]
#> id value V1 V2
#> 1: 1 2 1 a
#> 2: 1 7 1 a
#> 3: 1 2 2 b
#> 4: 1 7 2 b
#> 5: 1 2 3 b
#> 6: 1 7 3 b
#> 7: 2 4 1 a
#> 8: 2 1 1 a
#> 9: 2 4 2 b
#> 10: 2 1 2 b
#> 11: 2 4 3 b
#> 12: 2 1 3 b
#> 13: 3 3 1 a
#> 14: 3 3 2 b
#> 15: 3 3 3 b