Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mgirlich opened this issue May 19, 2021 · 0 comments · Fixed by #247
Closed

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

mgirlich opened this issue May 19, 2021 · 0 comments · Fixed by #247

Comments

@mgirlich
Copy link
Collaborator

mgirlich commented May 19, 2021

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)

@mgirlich mgirlich changed the title grouping variable cannot be selected after transmute() group_by() + transmute() has multiple issues May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant