Skip to contents

Use 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)
)

Arguments

df

a dataframe of data

value

string of column that qunatiles will be calculated for

weight

string of column that will be used to calculate quantiles with

quantiles

vector of quantiles that should be returned - default is `c(0, .25, .5, .75, 1)` quantiles

Value

a dataframe with qunatiles - in wide format

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()
} # }