Calculate quantiles for groups of pre-aggregated metrics.
group_wtd_quantiles.RdUse this function to calculate qunatiles for data that has already been aggregated and collapsed into counts. Avoids the need to expand data then compute quantiles.
Usage
group_wtd_quantiles(
df,
value,
weight = "count",
quantiles = c(0, 0.25, 0.5, 0.75, 1)
)Examples
if (FALSE) { # \dontrun{
df = data.frame(group = c(rep(1, 4)
,rep(2, 3))
,days = c(round(runif(4, 0, 20), 0)
,round(runif(3, 0, 20), 0))
,count = c(round(runif(4, 0, 5), 0)
,round(runif(3, 0, 5), 0)))
df %>%
group_by(group) %>%
nest() %>%
mutate(qauntiles = map(data, ~group_wtd_quantiles(.x, value = "days", weight = "count"))) %>%
unnest(cols = c(qauntiles)) %>%
select(!data)
#alternative calucaltion by expansion
rep(df$days[1:4], df$count[1:4]) %>% summary()
} # }