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