data.table Syntax & Keys
Maya's single bakery has grown into a chain. Her till now logs hundreds of thousands of sales a week across every shop, and the tidy dplyr pipelines that felt instant on one shop's data have started to crawl. data.table is R's tool for exactly this moment: the same filter, compute and group you already know, written in one compact bracket and engineered to fly on millions of rows.
Below is a week of sales from two of her shops. Press Run to keep just the Austin rows, your first look at the bracket in action.
By the end of this lesson you will be able to:
- Read the
DT[i, j, by]grammar and name what each of the three slots does - Filter rows in
i, compute and select columns inj, and aggregate per group withby - Set a key with
setkey()and use it for lightning-fast lookups and joins
Prerequisites: you can run R, and you know the dplyr verbs and the pipe from The dplyr Verbs. We define every data.table idea by translating its dplyr equivalent, so no prior data.table experience is needed.
DT[i, j, by]: take rows, do something, by group
Almost everything in data.table happens inside one square bracket with three slots, read left to right like a sentence: **DT[i, j, by]** means "take the rows i, compute j, grouped by by."
- **
i* = which rows* (a condition, like dplyr'sfilter()). - **
j* = what to compute* (select columns, build new ones, or summarise them, likeselect()plusmutate()plussummarise()). - **
by* = grouped how* (split into groups and runjinside each, likegroup_by()).
Let us build Maya's sales as a data.table. Each lesson runs in a fresh R session, so we create the data right here. The one setup line that matters is setDTthreads(1): it just tells data.table to use a single core, which is all the in-browser R has.
A data.table is also a data frame, so anything that expects a data frame still works. The difference is everything you can now do inside that bracket. The flow below is the map we will follow for the rest of the lesson.