convert dplyr join syntax into pure data.table syntax

后端 未结 2 1860
陌清茗
陌清茗 2020-12-09 07:06

I am learning data.table. I have difficulty converting the dplyr join syntax. Can you please recommend the data.table equivalence for the following test cases?



        
2条回答
  •  温柔的废话
    2020-12-09 07:40

    setkey(dtOrder, ProductID)
    

    (1-2)

    # this will be literally what you wrote
    
    dtProduct[dtOrder,
              list(OrderID, ProductID, ProductName, Qty, Price, ExtPrice=Qty*Price),
              nomatch = 0 # or omit this to get (2)
             ]
    
    # but I think you'd be better off with this
    dtProduct[dtOrder][, ExtPrice := Qty*Price][]
    

    (3)

    # you can again take the literal direction:
    dtProduct[dtOrder][!is.na(ProductName)][,
              list(OrderID, ProductID, ProductName, Qty)]
    
    # but again I think you'd be better off with
    dtOrder[!dtProduct]
    

    (4-5)

    dtProduct[dtOrder, nomatch = 0][,
              list(OrderCount=.N, TotalQty=sum(Qty), TotalSales=sum(Qty*Price)),
              by = list(ProductID, ProductName)][
              order(-TotalSales)]
    

提交回复
热议问题