In R, working in the tidyverse:
My data sources change. There's a column which is only present some weeks. When it is, I want to summarize it. Using iris
as an example, suppose that Sepal.Width
is sometimes missing. Conceptually, I want a function like this
library(tidyverse)
summIris <- function(irisDf){
irisDf %>%
group_by(Species) %>%
summarise_ifPresent(
Sepal.Length = mean(Sepal.Length),
Sepal.Width = mean(Sepal.Width))
}
Which'd return
R > summIris(iris )
# 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
> summIris(iris %>% select(- Sepal.Width ))
# A tibble: 3 x 2
Species Sepal.Length
<fct> <dbl>
1 setosa 5.01
2 versicolor 5.94
3 virginica 6.59
I could work around by wrapping the logic in if else
. But is there something more concise and elegant?
summarize_at
allows you to define on which columns you execute the summary, and you can use starts_with
, ends_with
, matches
, or contains
to dynamically select columns.
library(dplyr)
iris %>%
group_by(Species) %>%
summarize_at(vars(starts_with("Sepal")), funs(mean(.)))
# # 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
iris %>%
select(-Sepal.Length) %>%
group_by(Species) %>%
summarize_at(vars(starts_with("Sepal")), funs(mean(.)))
# # A tibble: 3 x 2
# Species Sepal.Width
# <fct> <dbl>
# 1 setosa 3.43
# 2 versicolor 2.77
# 3 virginica 2.97
Another one also works but gives a warning with unfound columns:
iris %>%
select(-Sepal.Length) %>%
group_by(Species) %>%
summarize_at(vars(one_of(c("Sepal.Width", "Sepal.Length"))), funs(mean(.)))
# Warning: Unknown columns: `Sepal.Length`
# # A tibble: 3 x 2
# Species Sepal.Width
# <fct> <dbl>
# 1 setosa 3.43
# 2 versicolor 2.77
# 3 virginica 2.97