R dplyr: how to use ... with summarize(across()) when ... will refer to a variable name within the data?
Asked Answered
S

3

7

I want to have a flexible function using summarize in which:

  1. the aggregation function is given by user
  2. the aggregation function might use further arguments which refer to variables within the data itself.

A good example is the user providing fun=weighted.mean() and specifying the weight argument w.

For now, I am trying with the .... The problem is that I don't find a way to have that ... refer to a variable within the data-frame? The example below is given using across(), but the same happens if I use instead summarize_at()

Thanks!!

library(tidyverse)
fo1 <- function(df, fun=mean, ...){
  df %>% 
    group_by(Species) %>% 
    summarise(across(starts_with("sepal"), fun, ...))
}

fo1(iris)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.01        3.43
#> 2 versicolor         5.94        2.77
#> 3 virginica          6.59        2.97
fo1(iris, fun=weighted.mean)
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.01        3.43
#> 2 versicolor         5.94        2.77
#> 3 virginica          6.59        2.97
fo1(iris, fun=weighted.mean, w=Petal.Length)
#> Error: Problem with `summarise()` input `..1`.
#> x object 'Petal.Length' not found
#> ℹ Input `..1` is `across(starts_with("sepal"), fun, ...)`.
#> ℹ The error occurred in group 1: Species = "setosa".
fo1(iris, fun=weighted.mean, w=.data$Petal.Length)
#> Error: Problem with `summarise()` input `..1`.
#> x 'x' and 'w' must have the same length
#> ℹ Input `..1` is `across(starts_with("sepal"), fun, ...)`.
#> ℹ The error occurred in group 1: Species = "setosa".

Created on 2020-11-10 by the reprex package (v0.3.0)

Stretto answered 11/11, 2020 at 0:28 Comment(0)
C
3

enquos will return a list of quoted expressions. The unquote-splice operator, !!!, will unquote each element as an argument to the function call.

library(tidyverse)

fo1 <- function(df, fun = mean, ...) {
  df %>% 
    summarise(across(starts_with("sepal"), fun, !!!enquos(...)))
}

iris %>%
  group_by(Species) %>%
  fo1(fun = weighted.mean, w = Petal.Length, na.rm = TRUE)
#> # A tibble: 3 x 3
#>   Species    Sepal.Length Sepal.Width
#>   <fct>             <dbl>       <dbl>
#> 1 setosa             5.02        3.44
#> 2 versicolor         5.98        2.79
#> 3 virginica          6.64        2.99

See here for more info.

Clingy answered 12/11, 2020 at 0:23 Comment(0)
S
2

You need to pass the exact value of additional arguments. .data$Petal.Length is NULL.

library(dplyr)

fo1 <- function(df, fun=mean, ...){
  df %>% 
    summarise(across(starts_with("sepal"), fun, ...))
}


fo1(iris, fun=weighted.mean, w= iris$Petal.Length)
#  Sepal.Length Sepal.Width
#1     6.180167    2.970197
Seleucid answered 11/11, 2020 at 1:10 Comment(5)
thanks Ronak! Unfortunately this would fail if the data was grouped for example! I updated the example to make that constraint explicit.Stretto
I see. Yeah, that's true. But I think weighted.mean is a unique example where we have additional arguments in the form of a vector. Usually the additional arguments would be of the form na.rm = TRUE which can be applied directly with ....Seleucid
Use !!!enquos(...) instead of ... inside summarize. Then it can handle iris %>% group_by(Species) %>% fo1(fun = weighted.mean, w = Petal.Length, na.rm = TRUE).Clingy
@Paul: this is definitely the best solution so far.Anesthesiology
@Clingy I think you got actually THE answer! Want to make your comment an answer?Stretto
A
2

This is ugly, but works.

> fo1 <- function(df, fun=mean, ...){
+   w <- df %>% pull(...)
+   df %>% 
+     summarise(across(starts_with("Sepal"), fun, w))
+ }
> fo1(iris, fun=weighted.mean, Petal.Length)
  Sepal.Length Sepal.Width
1     6.180167    2.970197

Following on from Paul's suggestion in the comments above, this seems to be a general solution:

fo1 <- function(df, fun=mean, ...){
  df %>% 
    summarise(across(starts_with("Sepal"), fun, !!!enquos(...)))
}
> fo1(iris, fun=weighted.mean, Petal.Length)
  Sepal.Length Sepal.Width
1     6.180167    2.970197
> fo1(iris, fun=mean)
  Sepal.Length Sepal.Width
1     5.843333    3.057333

I tried several combinations of !!, !!!, enquo() and enquos() but must have missed that one.

Anesthesiology answered 11/11, 2020 at 1:12 Comment(2)
thanks Limey! Unfortunately this assume w is always there, but fails if one uses a function that does not need it!Stretto
I agree. That's why it's ugly! :)Anesthesiology

© 2022 - 2025 — McMap. All rights reserved.