I'm working with the following sample data:
library(data.table)
library(dtplyr)
library(dplyr)
df = data.frame(a = c(1,1,3), b = c(4:6))
names(df) = c('aa aa', 'bb bb')
df_dt = lazy_dt(df)
df_dt %>% group_by(`aa aa`) %>% slice(n())
However, I encountered this error:
Error in .checkTypos(e, names_x) :
Object '`aa aa`' not found amongst aa aa, bb bb
Could you help me understand if I've overlooked something or made an error? Thank you.
This happens because
df_dtis not adata.frame. The functionsgroup_byandslicework on data.frame. Butdf_dtis not adata.frame. You can check it usingclass(df_dt).If it is a
data.frameortibble, then only the code will work. Butdf_dt$parentis adata.frame.Try to modify the code
This will give you desired result.
Edit: After the follow-up question about using
dplyr::slicewithout subsetting, I want to clarify further.The
dplyr::clarifyexpects the input to be adata.frame. If you runclass(df_dt), the output isThis means
dplyr::slicewill not work ondf_dtobject. But if you run `class(df_dt$parent), the output isThat means, you can run
dplyr::sliceon this object. When I attempted it, the output isI hope it helps.