Expand grid of data.tables
egdt(dt1, dt2, quiet)
a data.table that expands combinations of rows in dt1 and dt2.
Merging works mostly similarly for data.table and data.table. However, for data.table the merge must be done by one or more columns. This means that the convenient way to expand all combinations of all rows in two data.frames is not available for data.tables. This functions provides that functionality. It always returns data.tables.
df1 <- data.frame(a=1:2,b=3:4)
df2 <- data.frame(c=5:6,d=7:8)
merge(df1,df2)
#> a b c d
#> 1 1 3 5 7
#> 2 2 4 5 7
#> 3 1 3 6 8
#> 4 2 4 6 8
library(data.table)
## This is not possible
if (FALSE) { # \dontrun{
merge(as.data.table(df1),as.data.table(df2),allow.cartesian=TRUE)
} # }
## Use egdt instead
egdt(as.data.table(df1),as.data.table(df2),quiet=TRUE)
#> a b c d
#> <int> <int> <int> <int>
#> 1: 1 3 5 7
#> 2: 1 3 6 8
#> 3: 2 4 5 7
#> 4: 2 4 6 8
## Dimensions are conveniently listed for interactive use
res <- egdt(as.data.table(df1),as.data.table(df2))
#> data nrows ncols
#> <char> <int> <int>
#> 1: dt1 2 2
#> 2: dt2 2 2
#> 3: result 4 4