12 data.table
- data.table
- dtplyr data.table back-end for ‘dplyr’
12.1 dtplyr 语法
DTPLYR 使用 DPLYR 的语法s实现 data.table 的速度;编写 DPLYR(和 TidyR)代码,DTPLYR 将其转换为等效的 Data.Table。
Show the code
mtcars2 <- lazy_dt(mtcars)
Show the code
dtplyr <- mtcars2 %>%
filter(wt < 5) %>%
mutate(l100k = 235.21 / mpg) %>% # liters / 100 km
group_by(cyl) %>%
summarise(l100k = mean(l100k))
dtplyr
#> Source: local data table [3 x 2]
#> Call: `_DT1`[wt < 5][, `:=`(l100k = 235.21/mpg)][, .(l100k = mean(l100k)),
#> keyby = .(cyl)]
#>
#> cyl l100k
#> <dbl> <dbl>
#> 1 4 9.05
#> 2 6 12.0
#> 3 8 14.9
#>
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results
dtplyr %>% show_query()
#> `_DT1`[wt < 5][, `:=`(l100k = 235.21/mpg)][, .(l100k = mean(l100k)),
#> keyby = .(cyl)]
dtplyr %>% as_tibble()
cyl | l100k |
---|---|
4 | 9.048898 |
6 | 11.970180 |
8 | 14.871885 |
Show the code
dt <- data.table::as.data.table(mtcars)
dt[wt<5][, `:=`(l100k = 235.21/mpg)][, .(l100k = mean(l100k)), keyby = .(cyl)]
cyl | l100k |
---|---|
4 | 9.048898 |
6 | 11.970180 |
8 | 14.871885 |
12.2 data.table 语法
DT[i, j, by]
## R: i j by
## SQL: where | order by select | update group by
https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html