To use facilities provided by data.table, but do not have to load data.table package.
in_dt(.data, ...)
as_dt(.data)
The as_dt
could turn any data frame to data.table class. If the data is
not a data frame, return error.
The in_dt
function creates a virtual environment in data.table, it could be
piped well because it still follows the principals of tidyfst, which are: (1) Never
use in place replacement and (2) Always recieves a data frame (data.frame/tibble/data.table)
and returns a data.table. Therefore, the in place functions like :=
will still
return the results.
iris %>% as_dt()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <num> <num> <num> <num> <fctr>
#> 1: 5.1 3.5 1.4 0.2 setosa
#> 2: 4.9 3.0 1.4 0.2 setosa
#> 3: 4.7 3.2 1.3 0.2 setosa
#> 4: 4.6 3.1 1.5 0.2 setosa
#> 5: 5.0 3.6 1.4 0.2 setosa
#> ---
#> 146: 6.7 3.0 5.2 2.3 virginica
#> 147: 6.3 2.5 5.0 1.9 virginica
#> 148: 6.5 3.0 5.2 2.0 virginica
#> 149: 6.2 3.4 5.4 2.3 virginica
#> 150: 5.9 3.0 5.1 1.8 virginica
iris %>% in_dt(order(-Sepal.Length),.SD[.N],by=Species)
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> <fctr> <num> <num> <num> <num>
#> 1: virginica 4.9 2.5 4.5 1.7
#> 2: versicolor 4.9 2.4 3.3 1.0
#> 3: setosa 4.3 3.0 1.1 0.1