Lets say I'd like to calculate the mean
, min
and max
for an arbitraty amount of groups within a custom function.
The toy data looks like this:
library(tidyverse)
df <- tibble(
Gender = c("m", "f", "f", "m", "m",
"f", "f", "f", "m", "f"),
IQ = rnorm(10, 100, 15),
Other = runif(10),
Test = rnorm(10),
group2 = c("A", "A", "A", "A", "A",
"B", "B", "B", "B", "B")
)
To achieve this for two groups (gender, group2) I could use
df %>%
gather(Variable, Value, -c(Gender, group2)) %>%
group_by(Gender, group2, Variable) %>%
summarise(mean = mean(Value),
min = min(Value),
max = max(Value))
which could be integrated with the new curly-curly
operators from rlang
with
descriptive_by <- function(data, group1, group2) {
data %>%
gather(Variable, Value, -c({{ group1 }}, {{ group2 }})) %>%
group_by({{ group1 }}, {{ group2 }}, Variable) %>%
summarise(mean = mean(Value),
min = min(Value),
max = max(Value))
}
Usually, I would assume that I could substitute the specified groups with ...
, but it doesn't seem to work like that
descriptive_by <- function(data, ...) {
data %>%
gather(Variable, Value, -c(...)) %>%
group_by(..., Variable) %>%
summarise(mean = mean(Value),
min = min(Value),
max = max(Value))
}
as it returns the error
Error in map_lgl(.x, .p, ...) : object 'Gender' not found