dtplyr cannot be used with illegal name

51 Views Asked by At

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.

1

There are 1 best solutions below

2
Eva On

This happens because df_dt is not a data.frame. The functions group_by and slice work on data.frame. But df_dt is not a data.frame. You can check it using class(df_dt).

If it is a data.frame or tibble, then only the code will work. But df_dt$parent is a data.frame.

Try to modify the code

df_dt$parent |> group_by(`aa aa`) %>% slice(n())

This will give you desired result.

Edit: After the follow-up question about using dplyr::slice without subsetting, I want to clarify further.

The dplyr::clarify expects the input to be a data.frame. If you run class(df_dt), the output is

class(df_dt)
[1] "dtplyr_step_first"
[2] "dtplyr_step"

This means dplyr::slice will not work on df_dt object. But if you run `class(df_dt$parent), the output is

class(df_dt$parent)
[1] "data.table" "data.frame"

That means, you can run dplyr::slice on this object. When I attempted it, the output is

df_dt$parent |> group_by(`aa aa`) %>% slice(n())
# A tibble: 2 × 2
# Groups:   aa aa [2]
  `aa aa` `bb bb`
    <dbl>   <int>
1       1       5
2       3       6

I hope it helps.