Outline
Original Post
data.table is a popular library in R, powerful, efficient. But the join/merge syntaxes in data.table are not so straightforward.
The common syntax as below which referred 〈JOINing data in R using data.table〉.
Join Type | DT | data.table::merge() |
Inner | X[Y, nomatch = 0] | merge(X, Y, all=FALSE) |
Left Outer | Y[X] | merge(X, Y, all.x=TRUE) |
Right Outer | – | merge(X, Y, all.y=TRUE) |
Full Outer | – | merge(X, Y, all=TRUE) |
Full Outer Where Null (Not Inner) | – | merge(X, Y, all=TRUE) |
Cross Join | – | – |
How About Cross Join?
Cross join is a method in SQL. This method will cause data quantity to increase significantly, but it is extremely helpful in the right case.

Copy, Paste, And Work
Add the function to your codebase to upgrade your programming efficiency.
dt_cross_join <- function(a, b){
cj = CJ(1:nrow(a),1:nrow(b))
cbind(a[cj[[1]],],b[cj[[2]],])
}
Related Posts
〈Learn Python And R On DataCamp. Start Your Data Science Career.〉