data.table Cross Join In R

Outline

    Original Post

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



    Related Posts

    Aron

    A data scientist working in a retail company, with experience in web design and marketing. Recently dives into cryptocurrency and quantitative investing.

    facebook telegram
    Content Protection by DMCA.com

    Leave a Reply

    • Required fields are market * .
    • Your email address will not be published.
    • Please ensure your email address is correct, then you will get a notification once a new comment reply.

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