Performing fast aggregation with a data.table
Another advantage of a data.table
is that we can easily aggregate data without the help of additional packages. In this recipe, we will illustrate how to perform data aggregation using data.table
.
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 a data.frame
and a data.table
into your R environment.
How to do it…
Perform the following steps to perform data aggregation over data.table
:
First, we can average the price of orders in total:
> order.dt[,mean(na.omit(Price))] [1] 2012.119
Next, we can average the amount of orders per user:
> mean.price.by.user <- order.dt[,mean(na.omit(Price)), User] > head(mean.price.by.user) User V1 1: U312622727 934.00 2: U239012343 1298.75 3: U10007697373 247.00 4: U296328517 745.00 5: U300884570 249.00 6: U451050374 1780.00
In order to name the aggregated data, we can surround...