O'Reilly logo

Data Analysis with R - Second Edition by Tony Fischetti

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The i in DT [i, j, by]

In this subsection, we focus on the first third of  data.table square bracket syntax. Specifically, this is the third responsible for selecting and filtering rows: the WHERE clause, using SQL parlance.

Take the four simplest examples in the following code. These select (a) the first row, (b) the first, second, and third rows, (c) the first row (again), and second row, and (d) the last row. This is illustrated in the following code:

> tracks[1, ]> tracks[1:3, ]> tracks[c(1,1,2)]> tracks[.N, ]

There are two main things to note here. We can see the first by taking a closer look at the third statement. Note that, unlike the others, we don’t use a comma at the end. If this were a data.frame, the result would be a selection ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required