Merging data with dplyr

In a SQL operation, we can perform a join operation to combine two different datasets. In dplyr, we have the same join operation that enables us to merge data easily. In this recipe, we explain how join works in dplyr.

Getting ready

Ensure that you completed the Enhancing a data.frame with a data.table recipe to load purchase_view.tab and purchase_order.tab as both data.frame and data.table into your R environment.

How to do it…

Perform the following steps to merge data with dplyr:

  1. First, we generate a product.dt data table by calculating the amount of purchased items:
    > product.dt <- order.dt[,.(Buy = length(Action)),by=Product]
    > head(product.dt[order(-Buy)])
              Product Buy
    1:    P0005772981 821
    2:    P0024239865 729
    3: P0004607050 ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.