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
.
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.
Perform the following steps to merge data with dplyr
:
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 584 4: P0003425855 552 5: P0014252066 438 6: P0006587250014 357
Next, we can calculate the number of views by product, and put the calculation result into
view.dt
:> view.dt <- purchase.dt[, .(n_views = length...