Is it possible to have conditional statements operate on different parts of dplyr::summarize()?
Imagine I am working with the iris
data and outputting a summary and I want to only include the mean of Sepal.Length when requested. So I could do something like:
data(iris)
include_length = T
if (include_length) {
iris %>%
group_by(Species) %>%
summarize(mean_sepal_width = mean(Sepal.Width), mean_sepal_length = mean(Sepal.Length))
} else {
iris %>%
group_by(Species) %>%
summarize(mean_sepal_width = mean(Sepal.Width))
}
But is there a way to implement the conditional within the pipeline so that it does not need to be duplicated?