Lesson 1 of 3

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 in j, and aggregate per group with by
  • 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.

The whole grammar

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's filter()).
  • **j* = what to compute* (select columns, build new ones, or summarise them, like select() plus mutate() plus summarise()).
  • **by* = grouped how* (split into groups and run j inside each, like group_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.

RInteractive R
library(data.table) setDTthreads(1) # the in-browser R runs on one core # A week of sales from two shops, one row per sale: sales <- data.table( shop = c("Austin", "Austin", "Denver", "Denver", "Austin", "Denver", "Austin", "Denver"), item = c("Sourdough", "Bagel", "Sourdough", "Croissant", "Croissant", "Bagel", "Sourdough", "Sourdough"), units = c(18, 40, 22, 15, 27, 30, 20, 12), revenue = c(81, 60, 99, 60, 108, 45, 90, 54) ) sales #> shop item units revenue #> 1: Austin Sourdough 18 81 #> 2: Austin Bagel 40 60 #> 3: Denver Sourdough 22 99 #> 4: Denver Croissant 15 60 #> 5: Austin Croissant 27 108 #> 6: Denver Bagel 30 45 #> 7: Austin Sourdough 20 90 #> 8: Denver Sourdough 12 54

  

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.