The goal of this package is to offer an alternative way of expressing common operations with data.table
without sacrificing the performance optimizations that it offers. The foundation for the data manipulation verbs is the dplyr
package, which also advocates the piping operator from the magrittr
package. The rlang
package powers most of this package’s functionality, which means that tidy evaluation can also be supported. There are other resources that provide comprehensive descriptions of these packages, so they will not be explained here.
Even though using data manipulation verbs can improve expressiveness in some cases, this is not always true, so using the traditional data.table
syntax might still be preferable in many situations. Unfortunately, it is not entirely straightforward to use the verbs without considerable knowledge of the syntax understood by data.table
, as well as its optimizations and special symbols. We will return to the importance of this at the end.
In order to resemble SQL syntax more closely, a couple of verb aliases are also defined:
where
, being equivalent to dplyr
’s filter
.order_by
, being equivalent to dplyr
’s arrange
.The examples here will be working with the mtcars
data to describe single-table verbs:
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
#> 32: 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2
Two-table joining verbs have their own vignette.
The foundation for this package is building expressions that are almost entirely delegated to data.table
. These expressions are built by parsing the input of the different verbs. In order to explicitly show when an expression is being built and subsequently evaluated, we use 3 delimiters:
start_expr
end_expr
chain
These also serve as visual reminders that we are not dealing directly with data.table
s during the process. We capture the input data.table
and start the process with start_expr
, and evaluate the final expression with end_expr
. Using chain
is equivalent to calling end_expr
immediately followed by start_expr
.
In order to print more information regarding the expression-building process, we can either set options(table.express.verbose = TRUE)
, or pass .verbose = TRUE
to start_expr
or chain
.
The arrange
/order_by
verbs add an expression with order
to the frame, and let data.table
handle it as usual:
#> Expression after where.ExprBuilder(...):
#> .DT_[order(mpg, -cyl)]
#> Using captured data.table for evaluation.
#> Evaluating:
#> .DT_[order(mpg, -cyl)]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#> 2: 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#> ---
#> 31: 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> 32: 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
We see here that the built expression includes a .DT_
pronoun. When the expression is evaluated, the captured data.table
is assigned to the evaluation environment as said pronoun.
Even though selecting a subset of columns is a common operation, it may be undesirable to do so when working with data.table
s because they always lead to deep data copies. Given that data.table
supports modification by reference, these copies are necessary. For example, the following would require copying all columns between mpg
and disp
, since any of them could be modified by reference afterwards.
Therefore, it might be preferable to use mutation verbs to remove columns that are not of interest.
With that said, the select
verb can be used as usual:
#> .DT_[, list(mpg, am)]
#> mpg am
#> 1: 21.0 1
#> 2: 21.0 1
#> ---
#> 31: 15.0 1
#> 32: 21.4 1
To maintain consistency, even single columns are kept as data.table
s:
#> .DT_[, list(mpg)]
#> mpg
#> 1: 21.0
#> 2: 21.0
#> ---
#> 31: 15.0
#> 32: 21.4
Calls to tidyselect
’s helpers or to :
are handled specially internally:
#> mpg cyl disp gear carb
#> 1: 21.0 6 160 4 4
#> 2: 21.0 6 160 4 4
#> ---
#> 31: 15.0 8 301 5 8
#> 32: 21.4 4 121 4 2
#> mpg am
#> 1: 21.0 1
#> 2: 21.0 1
#> ---
#> 31: 15.0 1
#> 32: 21.4 1
Tidy evaluation and the .parse
argument can also aid in cases where certain parts of the frame were computed programmatically:
#> mpg am
#> 1: 21.0 1
#> 2: 21.0 1
#> ---
#> 31: 15.0 1
#> 32: 21.4 1
The transmute
verb simply wraps everything in its ellipsis in a call to list
and assigns the expression to the frame’s j
.
#> .DT_[, list(foo = mpg * 2, bar = exp(cyl))]
#> foo bar
#> 1: 42.0 403.42879
#> 2: 42.0 403.42879
#> ---
#> 31: 30.0 2980.95799
#> 32: 42.8 54.59815
Even though select
can be used for the same (given the way data.table
handles j
), the simplicity of transmute
’s internals makes it preferable when no tidyselect
helpers are needed, since it avoids extra function calls and can reduce overhead significantly in special circumstances.
The mutate
verb builds an expression with :=
in order to perform assignment by reference by default. This can be avoided by passing .by_ref = FALSE
to end_expr
, which will use data.table::copy
before assigning .DT_
:
#> .DT_[, `:=`(mpg = mpg/2, hp = log(hp))]
DT %>%
start_expr %>%
mutate(mpg = mpg / 2, hp = log(hp)) %>%
end_expr(.by_ref = FALSE) %>% {
invisible(print(.))
}
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 10.5 6 160 4.700480 3.90 2.620 16.46 0 1 4 4
#> 2: 10.5 6 160 4.700480 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 7.5 8 301 5.814131 3.54 3.570 14.60 0 1 5 8
#> 32: 10.7 4 121 4.691348 4.11 2.780 18.60 1 1 4 2
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
#> 32: 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2
It can also support expressions that already contain :=
:
new_vars <- c("x", "y")
DT %>%
start_expr %>%
mutate(!!new_vars := .(1, 2), .unquote_names = FALSE) %>%
end_expr(.by_ref = FALSE) %>% {
invisible(print(.))
}
#> mpg cyl disp hp drat wt qsec vs am gear carb x y
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1 2
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 1 2
#> ---
#> 31: 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8 1 2
#> 32: 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2 1 2
The where
/filter
verbs work with the i
part of the frame:
#> .DT_[vs == 1L & carb > 2L]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
#> 2: 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
DT %>%
start_expr %>%
select(mean_mpg = mean(mpg)) %>%
where(vs == 1L, carb > 2L, .collapse = `|`) %T>%
print %>%
end_expr
#> .DT_[vs == 1L | carb > 2L, .select_matching(.SD, mean_mpg = mean(mpg),
#> .negate = FALSE)]
#> mean_mpg
#> 1: 20.30741
The helper verb filter_sd
can be used to apply the same conditions to many columns, and it can use a special pronoun .COL
while specifying the expression, as well as tidyselect
helpers to choose .SDcols
(with caveats, see eager verbs):
#> .DT_[mpg > 20 & qsec > 20]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> 2: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> 2: 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> .DT_[mpg > 0 & am > 0]
#> .DT_[mpg > 0 & cyl > 0 & disp > 0 & hp > 0 & drat > 0 & wt >
#> 0 & qsec > 0 & vs > 0 & am > 0 & gear > 0 & carb > 0]
The filter_on
verb can be used to build an expression that specifies the on
argument of the frame. It accepts key-value pairs where each key is a column in the data, and each value is the corresponding value that the column should have to match:
#> .DT_[list(6, 0), on = c("cyl", "am")]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> 2: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> 3: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> 4: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
In order to support functionality similar to data.table
’s DT[, lapply(.SD, fun), .SDcols = c("...")]
syntax, 2 verbs are provided: mutate_sd
and transmute_sd
.
Starting with mutate_sd
, it modifies columns in .SDcols
by reference, and columns that are not part of .SDcols
are kept:
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 1318815734 403.42879 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 1318815734 403.42879 160 110 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 3269017 2980.95799 301 335 3.54 3.570 14.60 0 1 5 8
#> 32: 1967441884 54.59815 121 109 4.11 2.780 18.60 1 1 4 2
It would also be possible to remove several columns with mutate_sd
:
DT %>%
start_expr %>%
mutate_sd(c("mpg", "cyl"), NULL) %>%
end_expr(.by_ref = FALSE) %>% {
invisible(print(.))
}
#> disp hp drat wt qsec vs am gear carb
#> 1: 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 160 110 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 301 335 3.54 3.570 14.60 0 1 5 8
#> 32: 121 109 4.11 2.780 18.60 1 1 4 2
Additionally, mutate_sd
supports the special .COL
pronoun that symbolizes the column that should be modified:
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> 2: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> ---
#> 31: 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8
#> 32: 21.4 4 121 109 4.11 2.780 18.60 1 1 4 2
#> .DT_[, `:=`(c("cyl", "hp", "vs", "am", "gear", "carb"), .mutate_matching(.SD,
#> c("cyl", "hp", "vs", "am", "gear", "carb"), rlang::quos(as.integer(.COL))))]
On the other hand, transmute_sd
never modifies by reference, and supports special expressions to “build” what is chosen as .SDcols
. These expressions can use tidyselect
helpers, as well as another special pronoun: .COLNAME
:
#> disp drat
#> 1: 320 7.80
#> 2: 320 7.80
#> ---
#> 31: 602 7.08
#> 32: 242 8.22
#> disp drat
#> 1: 320 7.80
#> 2: 320 7.80
#> ---
#> 31: 602 7.08
#> 32: 242 8.22
# like dplyr's transmute_if
DT %>%
start_expr %>%
transmute_sd(is.numeric(.COL), .COL * 2) %>%
end_expr
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 42.0 12 320 220 7.80 5.24 32.92 0 2 8 8
#> 2: 42.0 12 320 220 7.80 5.75 34.04 0 2 8 8
#> ---
#> 31: 30.0 16 602 670 7.08 7.14 29.20 0 2 10 16
#> 32: 42.8 8 242 218 8.22 5.56 37.20 2 2 8 4
Both verbs also support a list of functions for multiple transformations, but mutate_sd
performs pairwise transformations, whereas transmute_sd
performs all combinations:
DT %>%
start_expr %>%
mutate_sd(contains("m"), .(round, -1L)) %>%
end_expr(.by_ref = FALSE) %>% {
invisible(print(.))
}
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> 1: 21 6 160 110 3.90 2.620 16.46 0 -1 4 4
#> 2: 21 6 160 110 3.90 2.875 17.02 0 -1 4 4
#> ---
#> 31: 15 8 301 335 3.54 3.570 14.60 0 -1 5 8
#> 32: 21 4 121 109 4.11 2.780 18.60 1 -1 4 2
#> min.mpg min.am max.mpg max.am mean.mpg mean.am
#> 1: 10.4 0 33.9 1 20.09062 0.40625
Since data.table
already supports this by means of its by
parameter, the group_by
verb simply parses its input and assigns it accordingly:
#> .DT_[, list(.N), by = list(gear)]
#> gear N
#> 1: 4 12
#> 2: 3 15
#> 3: 5 5
The key_by
verb does the same but sets the key of the result in order to sort:
#> .DT_[, list(.N), keyby = list(gear)]
#> gear N
#> 1: 3 15
#> 2: 4 12
#> 3: 5 5
The distinct
verb is a shortcut for select(.SD[1])
with a by
clause:
#> .DT_[, .SD[1L], by = list(vs, am)]
#> vs am mpg cyl disp hp drat wt qsec gear carb
#> 1: 0 1 21.0 6 160 110 3.90 2.620 16.46 4 4
#> 2: 1 1 22.8 4 108 93 3.85 2.320 18.61 4 1
#> 3: 1 0 21.4 6 258 110 3.08 3.215 19.44 3 1
#> 4: 0 0 18.7 8 360 175 3.15 3.440 17.02 3 2
A data.table
’s frame has 3 main elements: i
, j
, and by
. By default, the verbs defined in this package automatically start a new frame whenever they want to define one of these elements, but the current expression’s frame has already specified it; otherwise they add to the current frame. More complex expressions are thus supported by automatically chaining data.table
frames:
DT %>%
start_expr %>%
select(mean_mpg = mean(mpg)) %>%
where(hp > 50L) %>%
group_by(vs, am, gear) %>%
order_by(gear, -vs, am) %>%
filter(mean_mpg > 20) %T>%
print %>%
end_expr %>% {
invisible(print(., nrows = 10L))
}
#> .DT_[hp > 50L, .select_matching(.SD, mean_mpg = mean(mpg), .negate = FALSE),
#> by = list(vs, am, gear)][order(gear, -vs, am)][mean_mpg >
#> 20]
#> vs am gear mean_mpg
#> 1: 1 0 3 20.33333
#> 2: 1 0 4 21.05000
#> 3: 1 1 4 28.03333
#> 4: 0 1 4 21.00000
#> 5: 1 1 5 30.40000
If we wanted to be explicit about chaining whenever possible (see below), we could set options(table.express.chain = FALSE)
, which would lead to a warning being shown whenever a part of the query is replaced.
i
:
arrange
filter
filter_on
filter_sd
order_by
where
j
:
select
mutate
mutate_sd
transmute
transmute_sd
distinct
group_by
to specify by
key_by
to specify keyby
distinct
The automatic chaining mentioned above is not a problem in most situations. For example the following chains lead to the same result, and therefore have the same semantics:
#> [1] 42.0 42.0 45.6 42.8 48.8 45.6 64.8 60.8 67.8 43.0 54.6 52.0 60.8 42.8
#> [1] 42.0 42.0 45.6 42.8 48.8 45.6 64.8 60.8 67.8 43.0 54.6 52.0 60.8 42.8
However, these two chains have different semantics:
#> mpg
#> 1: 42.0
#> 2: 42.0
#> ---
#> 13: 60.8
#> 14: 42.8
#> Empty data.table (0 rows and 1 cols): mpg
As mentioned above, chain
can be used to chain expressions by evaluating the current one with end_expr
, and immediately capturing the resulting data.table
to start building a new expression. This can be helpful in situations where automatic chaining (or lack thereof) can lead to a change in the expression’s semantics:
#> .DT_[mpg > 40, list(mpg = mpg * 2)]
#> Empty data.table (0 rows and 1 cols): mpg
#> mpg
#> 1: 42.0
#> 2: 42.0
#> ---
#> 13: 60.8
#> 14: 42.8
In the following cases, the mentioned verbs use the captured data.table
eagerly during expression building:
filter_sd
when .SDcols
is a tidyselect
call or a predicate using .COL
.mutate_sd
when .SDcols
is a tidyselect
call or a predicate using .COL
.This can lead to unexpected results if we don’t keep in mind the expression that is built:
#> mpg cyl disp
#> 1: 21.0 6 160
#> 2: 21.0 6 160
#> ---
#> 31: 15.0 8 301
#> 32: 21.4 4 121
DT %>%
start_expr %>%
select(mpg:disp) %>%
transmute(ans = sqrt(mpg)) %>%
filter_sd(contains("m"), .COL > 0)
#> .DT_[, .select_matching(.SD, mpg:disp, .negate = FALSE)][mpg >
#> 0 & am > 0, list(ans = sqrt(mpg))]
The select
gets rid of am
, but filter_sd
sees the columns of DT
before any expression has been evaluated. Explicit chaining can help in these cases, capturing intermediate results:
DT %>%
start_expr %>%
select(mpg:disp) %>%
chain %>%
transmute(ans = sqrt(mpg)) %>%
filter_sd(contains("m"), .COL > 0)
#> .DT_[mpg > 0, list(ans = sqrt(mpg))]
Many of the verbs in this package try to help make code more concise whenever possible. However, some of the added convenience requires helper functions that aid with non-standard evaluation, which doesn’t always play nicely with the data.table
optimizations mentioned in the beginning. Therefore, it is worth keeping the built expressions as close to traditional data.table
syntax as possible. The verbs try to stick to this principle, but in order to build a simple output expression, they also require a simple input.
Let us illustrate this point with transmute_sd
, which can be used to compute summaries:
DT %>%
start_expr %>%
group_by(am, vs) %>%
transmute_sd(c("mpg", "disp"), mean) %>%
frame_append(verbose = TRUE) %T>%
print %>%
end_expr
#> .DT_[, c(lapply(.SD, mean)), by = list(am, vs), .SDcols = c(c("mpg",
#> "disp")), verbose = TRUE]
#> Finding groups using forderv ... 0.020s elapsed (0.010s cpu)
#> Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
#> Getting back original order ... 0.000s elapsed (0.000s cpu)
#> lapply optimization changed j from 'c(lapply(.SD, mean))' to 'list(mean(mpg), mean(disp))'
#> GForce optimized j to 'list(gmean(mpg), gmean(disp))'
#> Making each group and running j (GForce TRUE) ... 0.000s elapsed (0.000s cpu)
#> am vs mpg disp
#> 1: 1 0 19.75000 206.2167
#> 2: 1 1 28.37143 89.8000
#> 3: 0 1 20.74286 175.1143
#> 4: 0 0 15.05000 357.6167
We can see, for example, lapply optimization changed j ...
. Changing the expressions just a bit, without affecting its semantics, still hinders data.table
’s ability to recognize cases it can optimize:
DT %>%
start_expr %>%
group_by(am, vs) %>%
transmute_sd(c("mpg", "disp"), mean(.COL)) %>%
frame_append(verbose = TRUE) %T>%
print %>%
end_expr
#> .DT_[, .transmute_matching(.SD, .which = rlang::quo(c("mpg",
#> "disp")), .hows = rlang::quos(mean(x = .COL))), by = list(am,
#> vs), verbose = TRUE]
#> Finding groups using forderv ... 0.000s elapsed (0.000s cpu)
#> Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
#> Getting back original order ... 0.000s elapsed (0.000s cpu)
#> lapply optimization is on, j unchanged as '.transmute_matching(.SD, .which = rlang::quo(c("mpg", "disp")), .hows = rlang::quos(mean(x = .COL)))'
#> GForce is on, left j unchanged
#> Old mean optimization is on, left j unchanged.
#> Making each group and running j (GForce FALSE) ... The result of j is a named list. It's very inefficient to create the same names over and over again for each group. When j=list(...), any names are detected, removed and put back after grouping has completed, for efficiency. Using j=transform(), for example, prevents that speedup (consider changing to :=). This message may be upgraded to warning in future.
#>
#> collecting discontiguous groups took 0.000s for 4 groups
#> eval(j) took 0.002s for 4 calls
#> 0.000s elapsed (0.000s cpu)
#> am vs mpg disp
#> 1: 1 0 19.75000 206.2167
#> 2: 1 1 28.37143 89.8000
#> 3: 0 1 20.74286 175.1143
#> 4: 0 0 15.05000 357.6167
Corroborated by the statements j unchanged
, GForce FALSE
, etc. Nevertheless, a different syntax can enable additional functionality, for instance column renaming:
DT %>%
start_expr %>%
group_by(am, vs) %>%
transmute_sd(c("mpg", "disp"), .(avg = mean(.COL), min = min(.COL))) %>%
end_expr
#> am vs avg.mpg avg.disp min.mpg min.disp
#> 1: 1 0 19.75000 206.2167 15.0 120.3
#> 2: 1 1 28.37143 89.8000 21.4 71.1
#> 3: 0 1 20.74286 175.1143 17.8 120.1
#> 4: 0 0 15.05000 357.6167 10.4 275.8
Thus, in order to decide which syntax to use, we must be aware of what can be optimized by data.table
, but as a general rule of thumb, the simpler the better.
DT %>%
start_expr %>%
group_by(am, vs) %>%
transmute_sd(c("mpg", "disp"), .(min, max)) %>%
frame_append(verbose = TRUE) %T>%
print %>%
end_expr
#> .DT_[, c(min = lapply(.SD, min), max = lapply(.SD, max)), by = list(am,
#> vs), .SDcols = c(c("mpg", "disp")), verbose = TRUE]
#> Finding groups using forderv ... 0.000s elapsed (0.000s cpu)
#> Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
#> Getting back original order ... 0.000s elapsed (0.000s cpu)
#> lapply optimization changed j from 'c(min = lapply(.SD, min), max = lapply(.SD, max))' to 'list(min(mpg), min(disp), max(mpg), max(disp))'
#> GForce optimized j to 'list(gmin(mpg), gmin(disp), gmax(mpg), gmax(disp))'
#> Making each group and running j (GForce TRUE) ... 0.000s elapsed (0.000s cpu)
#> am vs mpg disp mpg disp
#> 1: 1 0 15.0 120.3 26.0 351
#> 2: 1 1 21.4 71.1 33.9 121
#> 3: 0 1 17.8 120.1 24.4 258
#> 4: 0 0 10.4 275.8 19.2 472