data.table Cross Join In R

目錄

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 Outermerge(X, Y, all.y=TRUE)
Full Outermerge(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.

 

SQL CROSS JOIN
Source:SQL CROSS JOIN

 

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.〉

A
Aron

Data analyst, but building WordPress websites, having experience in digital marketing. Recently dives into fighting and Python quantitative investment.

Content Protection by DMCA.com
Technical Notes

Leave a Reply

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