如何获取 data.table 格式的最终查询?
如果不可能:如何将查询从 dplyr 重写为 data.table ?
library(data.table)
library(dplyr)
library(dtplyr)
#Data
dt = structure(list(Year = c(2015L, 2016L, 2017L, 2015L, 2016L, 2017L),
Item = c("Soybeans", "Soybeans", "Soybeans", "Pulses, Total", "Pulses, Total", "Pulses, Total"),
Value = c(884688L, 829166L, 960640L, 2219455L, 2354696L, 2683772L)),
row.names = c(NA, -6L), class = "data.frame")
# query in dplyr
dt %>%
group_by(Year) %>%
summarise(Value = sum(Value), Item = "Total") %>%
bind_rows(., dt)
#conveert query to data.table
dtl=lazy_dt(dt)
dtl %>%
group_by(Year) %>%
summarise(Value = sum(Value), Item = "Total") %>%
bind_rows(., dtl) %>% show_query()
错误:参数 1 必须是数据帧或命名的原子向量。