Skip to content

Instantly share code, notes, and snippets.

@garethclews
Created April 9, 2020 10:23
Show Gist options
  • Save garethclews/7740b2c966610c5712abe9c50422a7a1 to your computer and use it in GitHub Desktop.
Save garethclews/7740b2c966610c5712abe9c50422a7a1 to your computer and use it in GitHub Desktop.
Ash help
weighted_count <- function(data, weights, ...) {
group_ <- enquos(...)
data %>%
group_by(!!!group_) %>%
summarise(amount = sum(.data[[weights]]), n = n())
}
iris %>% weighted_count("Petal.Length", Species)
## A tibble: 3 x 3
# Species amount n
# <fct> <dbl> <int>
# 1 setosa 73.1 50
# 2 versicolor 213 50
# 3 virginica 278. 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment