data.table Cross Join In R

<p>data.table is a popular library in R, powerful, efficient. But the join/merge syntaxes in data.table are not so straightforward.<br />The common syntax as below which referred  〈JOINing data in R using data.table〉.</p>

Original Post

〈data.table Cross Join In R〉

 


 

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 TypeDTdata.table::merge()
InnerX[Y, nomatch = 0]merge(X, Y, all=FALSE)
Left OuterY[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.〉

🚀 Unlock Ads-Free Experience At $5/year

✓ 14 days free trial ✓ Cancel anytime

46 thoughts on “data.table Cross Join In R”

Leave a Comment

Your email address will not be published. Required fields are marked *