Another package for data processing that has been useful to many is data.table. It works in a notably different way than dplyr. However, you’d use it for the same reasons, e.g. subset, grouping, update, ordered joins etc., but with key advantages in speed and memory efficiency. Like dplyr, the data objects are both data.frames and a package specific class.
 "data.table" "data.frame"
In general, data.table works with brackets as in base R data frames. However, in order to use data.table effectively you’ll need to forget the data frame similarity. The brackets actually work like a function call, with several key arguments. Consider the following notation to start.
Importantly: you don’t use the brackets as you would with data.frames. What i and j can be are fairly complex.
In general, you use i for filtering by rows.
x g y 1: 7 2 0.8433834 x g y 1: 7 2 0.8433834
You use j to select (by name!) or create new columns. We can define a new column with the := operator.
 6 7 3 10 1 9  6.922229 7.843383 3.708906 10.239135 1.481312 9.003890 g V1 1: 2 4.662348 2: 3 6.356398 x g y z 1: 6 1 0.922228829 6.922229 2: 7 2 0.843383416 7.843383 3: 3 3 0.708906139 3.708906 4: 10 1 0.239134972 10.239135 5: 1 2 0.481312103 1.481312 6: 9 3 0.003890211 9.003890
Because j is an argument, dropping columns is awkward.
 -0.922228829 -0.843383416 -0.708906139 -0.239134972 -0.481312103 -0.003890211 x g z 1: 6 1 6.922229 2: 7 2 7.843383 3: 3 3 3.708906 4: 10 1 10.239135 5: 1 2 1.481312 6: 9 3 9.003890
Data table does not make unnecessary copies. For example if we do the following…
DT2 and DT are just names for the same table. You’d actually need to use the copy function to make an explicit copy, otherwise whatever you do to DT2 will be done to DT.
A B q 1: 5 e 1 2: 4 d 1 3: 3 c 1 4: 2 b 1 5: 1 a 1
A B 1: 5 e 2: 4 d 3: 3 c 4: 2 b 5: 1 a
We can now attempt a ‘group-by’ operation, along with creation of a new variable. Note that these operations actually modify the dt object in place, a key distinction with dplyr. Fewer copies means less of a memory hit.
g V1 1: 1 17.161364 2: 2 9.324696 3: 3 12.712796
x g y z mysum 1: 6 1 0.922228829 6.922229 16 2: 7 2 0.843383416 7.843383 8 3: 3 3 0.708906139 3.708906 12 4: 10 1 0.239134972 10.239135 16 5: 1 2 0.481312103 1.481312 8 6: 9 3 0.003890211 9.003890 12
We can also create groupings on the fly. For a new summary data set, we’ll take the following approach- we create a grouping based on whether
g is a value of one or not, then get the mean and sum of
x for those two categories. The corresponding dplyr approach is also shown (but not evaluated) for comparison.
g mean_x sum_x 1: TRUE 8 16 2: FALSE 5 20
As mentioned, the reason to use data.table is speed. If you have large data or large operations it’ll be useful.
Joins can not only be faster but also easy to do. Note that the
i argument can be a data.table object itself. I compare its speed to the comparable dplyr’s left_join function.
We can use the setkey function to order a data set by a certain column(s). This ordering is done by reference; again, no copy is made. Doing this will allow for faster grouped operations, though you likely will only see the speed gain with very large data. The timing regards creating a new variable
The chin function returns a vector of the positions of (first) matches of its first argument in its second, where both arguments are character vectors. Essentially it’s just like the %in% function for character vectors.
Consider the following. We sample the first 14 letters 1000 times with replacement and see which ones match in a subset of another subset of letters. I compare the same operation to stringr and the stringi package whose functionality stringr using. They are both far slower than chin.
 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
If you use data.table for nothing else, you’d still want to consider it strongly for reading in large text files. The function fread may be quite useful in being memory efficient too. I compare it to readr.
The following demonstrates some timings from here. I reproduced it on my own machine based on 50 million observations. The grouped operations that are applied are just a sum and length on a vector.
By the way, never, ever use aggregate. For anything.
Another thing to note is that the tidy approach is more about clarity and code efficiency relative to base R, as well as doing important background data checks and returning more usable results. In practice, it likely won’t be notably faster except in some cases, like with aggregate.
Pipe with data.table
Piping can be done with data.table objects too, using the brackets, but it’s awkward at best.
Probably better to just use a standard pipe and dot approach if you really need it.
Faster and more memory-efficient methods are great to have. If you have large data this is one package that can help.
- For reading data
- Especially for group-by and joins.
- The syntax can be awkward
- It doesn’t work like a data.frame, which can be confusing
- Piping with brackets isn’t really feasible, and the dot approach is awkward
- Does not have its own ‘verse’, though many packages use it
If speed and/or memory is (potentially) a concern, data.table.
For interactive exploration, dplyr.
Piping allows one to use both, so no need to choose.
And on the horizon…
Faster dplyr alternatives
So we have data.table as a starting point for faster data processing operations, but there are others. The dtplyr package implements the data.table back-end for dplyr, so that you can seamlessly use them together. The newer package tidyfast works directly with a data.table object, but uses dplyr-esque functions. The following shows times for a counting unique arrival times in the nycflights13 flights data (336776 rows).
|a Median time in milliseconds to do a count of arr_time on nycflights::flights|
Just for giggles I did the same in Python with a pandas DataFrame, and it was notably slower than all of these options (almost 10x slower than standard dplyr). A lot of folks that use Python primarily think R is slow, but that is mostly because they don’t know how to effectively program with R for data science.
Install and load the data.table package.
Create the following data table.
Create a new object that contains only the ‘a’ group. Think back to how you use a logical to select rows.
Create a new object that is the sum of z grouped by x. You don’t need to name the sum variable.