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

manip_apply_syms ignores renamed vars when several funs are provided #4180

Closed
moodymudskipper opened this issue Feb 13, 2019 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@moodymudskipper
Copy link

@moodymudskipper moodymudskipper commented Feb 13, 2019

See reprex below.

library(tidyverse)
  
# renaming DISP on the fly does change the name of the output
mtcars %>%
  group_by(cyl) %>% summarize_at(vars(DISP = disp),median)
#> # A tibble: 3 x 2
#>     cyl  DISP
#>   <dbl> <dbl>
#> 1     4  108 
#> 2     6  168.
#> 3     8  350.

# but doesn't if several functions (see lower case "disp_mean" and "disp_median")
mtcars %>%
  group_by(cyl) %>% summarize_at(vars(DISP = disp),funs(mean,median))
#> # A tibble: 3 x 3
#>     cyl disp_mean disp_median
#>   <dbl>     <dbl>       <dbl>
#> 1     4      105.        108 
#> 2     6      183.        168.
#> 3     8      353.        350.

# replace the following part at the bottom of dplyr:::manip_apply_syms
# syms_names <- map_chr(syms, as_string)
# grid <- expand.grid(var = syms_names, call = names(funs))
# names(out) <- paste(grid$var, grid$call, sep = "_")
manip_apply_syms <- dplyr:::manip_apply_syms
body(manip_apply_syms)[[9]][[4]][[4]] <- quote({
  grid <- expand.grid(var = names(syms), call = names(funs))
  names(out) <- paste(grid$var, grid$call, sep = "_")
})
assignInNamespace(
  "manip_apply_syms", manip_apply_syms,
  ns = "dplyr", pos = "package:dplyr")
      
# still good
mtcars %>%
  group_by(cyl) %>% summarize_at(vars(DISP = disp),median)
#> # A tibble: 3 x 2
#>     cyl  DISP
#>   <dbl> <dbl>
#> 1     4  108 
#> 2     6  168.
#> 3     8  350.

# now good as well
mtcars %>%
  group_by(cyl) %>% summarize_at(vars(DISP = disp),funs(mean,median))
#> # A tibble: 3 x 3
#>     cyl DISP_mean DISP_median
#>   <dbl>     <dbl>       <dbl>
#> 1     4      105.        108 
#> 2     6      183.        168.
#> 3     8      353.        350.

Created on 2019-02-13 by the reprex package (v0.2.0).

The latter behavior makes more sense to me.

@echasnovski
Copy link
Contributor

@echasnovski echasnovski commented Feb 13, 2019

This was a design choice, as stated in #3100 .

@romainfrancois romainfrancois added this to the 0.8.1 milestone Mar 5, 2019
@romainfrancois romainfrancois self-assigned this Mar 5, 2019
@lock
Copy link

@lock lock bot commented Sep 1, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants