moodymudskipper · GitHub

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.

Read the original on github.com ↗