Skip to content

group_by() + transmute() has multiple issues #246

@mgirlich

Description

@mgirlich
library(dplyr, warn.conflicts = FALSE)
library(dtplyr)

df <- tibble(x = 1) %>% group_by(x)

# grouping variable cannot be selected
df %>% 
  lazy_dt() %>% 
  transmute(y = 2) %>% 
  .$vars
#> [1] "y"

df %>% 
  transmute(y = 2)
#> # A tibble: 1 x 2
#> # Groups:   x [1]
#>       x     y
#>   <dbl> <dbl>
#> 1     1     2

# transmuting a grouping variable 
df %>% 
  lazy_dt() %>% 
  transmute(x = x + 1, y = 2)
#> Source: local data table [1 x 3]
#> Groups: x
#> Call:   `_DT2`[, .(x = x + 1, y = 2), keyby = .(x)]
#> 
#>       x     x     y
#>   <dbl> <dbl> <dbl>
#> 1     1     2     2
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

df %>% 
  transmute(x = x + 1, y = 2)
#> # A tibble: 1 x 2
#> # Groups:   x [1]
#>       x     y
#>   <dbl> <dbl>
#> 1     2     2

# mutating works correctly
df %>% 
  lazy_dt() %>% 
  mutate(x = x + 1, y = 2)
#> Source: local data table [1 x 2]
#> Groups: x
#> Call:   copy(`_DT3`)[, `:=`(x = x + 1, y = 2), by = .(x)]
#> 
#>       x     y
#>   <dbl> <dbl>
#> 1     2     2
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

df %>% 
  mutate(x = x + 1, y = 2)
#> # A tibble: 1 x 2
#> # Groups:   x [1]
#>       x     y
#>   <dbl> <dbl>
#> 1     2     2

# grouping variable cannot be renamed
lazy_dt(tibble(x = 1, y = 2)) %>% 
  group_by(z = x) %>% 
  summarise(y = mean(y)) %>% 
  show_query()
#> `_DT4`[, .(y = mean(y)), keyby = .(z)]

tibble(x = 1, y = 2) %>% 
  group_by(z = x) %>% 
  summarise(y = mean(y))
#> # A tibble: 1 x 2
#>       z     y
#>   <dbl> <dbl>
#> 1     1     2

Created on 2021-05-19 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions