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

Feature request: at function in summarise, mutate, ... #3896

Closed
mgirlich opened this issue Oct 11, 2018 · 3 comments
Closed

Feature request: at function in summarise, mutate, ... #3896

mgirlich opened this issue Oct 11, 2018 · 3 comments

Comments

@mgirlich
Copy link

I often encounter the situation where summarise_at nearly does the job, but a different function is needed for one column. E.g.

library(dplyr)

mtcars %>% 
  group_by(cyl) %>% 
  summarise(
    mpg = mean(mpg),
    disp = mean(disp),
    hp = mean(hp),
    drat = mean(drat),
    wt = max(wt),
  )

Instead of the above code, it would be very nice to be able to write something like

mtcars %>% 
  group_by(cyl) %>% 
  summarise(
    at(c("mpg", "disp", "hp", "drat"), mean),
    wt = max(wt)
  )

Using the (not exported) function dplyr:::manip_at there basically already is a function nearly suited for the task

at <- function(.tbl, .vars, .funs, ...) {
  # .vars <- check_dot_cols(.vars, .cols)
  dplyr:::manip_at(
    .tbl, .vars, .funs, enquo(.funs), caller_env(), 
    .include_group_vars = TRUE, ...
  )
}


mtcars %>% 
  group_by(cyl) %>% 
  {summarise(
    .,
    !!!at(., c("mpg", "disp", "hp", "drat"), mean),
    wt = max(wt)
  )}

I would very much appreciate such a functionality integrated into dplyr.

@mgirlich mgirlich changed the title Feature request: Feature request: at function in summarise, mutate, ... Oct 11, 2018
@moodymudskipper
Copy link

moodymudskipper commented Oct 11, 2018

It would be nice if this could work :

mtcars %>% 
  group_by(cyl) %>% 
  summarize(!!!map(lst(mpg, disp, hp, drat),mean),
            wt = max(wt))

This "almost" works :

mtcars %>% 
  group_by(cyl) %>% 
  summarize(!!!with(.,map(lst(mpg, disp, hp, drat),mean)),
            wt = max(wt))
# # A tibble: 3 x 6
#     cyl      mpg     disp       hp     drat    wt
#   <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <dbl>
# 1     4 20.09062 230.7219 146.6875 3.596563 3.190
# 2     6 20.09062 230.7219 146.6875 3.596563 3.460
# 3     8 20.09062 230.7219 146.6875 3.596563 5.424  

And this works:

mtcars %>% 
  group_by(cyl) %>% 
  summarize(list(map_dfc(lst(mpg, disp, hp, drat),mean)),
            wt = max(wt)) %>%
  unnest

# A tibble: 3 x 6
#     cyl    wt   mpg  disp    hp  drat
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1     4  3.19  26.7  105.  82.6  4.07
# 2     6  3.46  19.7  183. 122.   3.59
# 3     8  5.42  15.1  353. 209.   3.23

@hadley
Copy link
Member

hadley commented May 27, 2019

Our current plan is to support something like this via #2326

@hadley hadley closed this as completed May 27, 2019
@lock
Copy link

lock bot commented Nov 23, 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 Nov 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants