calc_quantiles: Calculate quantiles across groups for a given column

View source: R/utils-calc-quantiles.R

calc_quantiles_by_groupR Documentation

Calculate quantiles across groups for a given column

Description

Calculate quantiles across groups for a given column

Calculate and insert columns containing arbitrary quantiles for a particular column

Calculate quantiles for the mean values (across years) of each value in the group given by grp_col for a term of years

Usage

calc_quantiles_by_group(
  df = NULL,
  grp_col = NULL,
  col = NULL,
  grp_names = grp_col,
  probs = c(0.05, 0.25, 0.5, 0.75, 0.95),
  include_mean = TRUE
)

calc_quantiles(
  df = NULL,
  col = NULL,
  probs = c(0.05, 0.25, 0.5, 0.75, 0.95),
  include_mean = TRUE
)

calc_term_quantiles(
  df = NULL,
  grp_col = NULL,
  col = NULL,
  min_yr = NA_real_,
  max_yr = NA_real_,
  probs = c(0.05, 0.25, 0.5, 0.75, 0.95),
  mean_multiplier = 1
)

Arguments

df

A data.frame with columns year, the string assigned to grp_col, and the string assigned to col

grp_col

The column name to use for grouping the data

col

A column name on which to perform the calculations. Must be in df or an error will be thrown

grp_names

The column name to use for labeling the grouped column. By default it is the same as the grouping column (grp_col).

probs

A vector of quantiles to pass to stats::quantile()

include_mean

If TRUE, include the mean in the output

min_yr

The minimum year in the term. If NA, the minimum value in the data.frame will be used

max_yr

The maximum year in the term. If NA, the maximum value in the data.frame will be used

mean_multiplier

This value will be multiplied by the mean of the values before the quantiles are calculated

Value

A data.frame containing the quantile values with one row per group represented by grp_col

A data.frame with a new column for each value in the probs vector

A single-row data.frame containing the quantile values of the run means

Examples

library(tibble)
library(dplyr)
library(purrr)
pq <- tribble(
  ~year, ~grp, ~val,
  2000,    1,  2.1,
  2001,    1,  3.4,
  2002,    1,  4.5,
  2003,    1,  5.6,
  2004,    1,  6.7,
  2000,    2,  3.1,
  2001,    2,  4.4,
  2002,    2,  5.5,
  2003,    2,  6.6,
  2004,    2,  8.7,
  2000,    3, 13.1,
  2001,    3, 14.4,
  2002,    3, 15.5,
  2003,    3, 16.6,
  2004,    3, 18.7)

probs <- c(0.05, 0.25, 0.5, 0.75, 0.95)

j <- calc_quantiles_by_group(pq,
                             grp_col = "year",
                             col = "val",
                             probs = probs)
library(tibble)
library(dplyr)
library(purrr)
pq <- tribble(
  ~year, ~grp, ~val,
  2000,    1,  2.1,
  2001,    1,  3.4,
  2002,    1,  4.5,
  2003,    1,  5.6,
  2004,    1,  6.7,
  2000,    2,  3.1,
  2001,    2,  4.4,
  2002,    2,  5.5,
  2003,    2,  6.6,
  2004,    2,  8.7,
  2000,    3, 13.1,
  2001,    3, 14.4,
  2002,    3, 15.5,
  2003,    3, 16.6,
  2004,    3, 18.7)

probs <- c(0.05, 0.25, 0.5, 0.75, 0.95)

yrs <- sort(unique(pq$year))
df <- pq %>%
  group_by(year) %>%
  group_map(~ calc_quantiles(.x, col = "val", probs = probs)) %>%
  map_df(~{.x}) %>%
  mutate(year = yrs) %>%
  select(year, everything())
library(tibble)
library(dplyr)
library(purrr)
pq <- tribble(
  ~year, ~grp, ~val,
  2000,    1,  2.1,
  2001,    1,  3.4,
  2002,    1,  4.5,
  2003,    1,  5.6,
  2004,    1,  6.7,
  2000,    2,  3.1,
  2001,    2,  4.4,
  2002,    2,  5.5,
  2003,    2,  6.6,
  2004,    2,  8.7,
  2000,    3, 13.1,
  2001,    3, 14.4,
  2002,    3, 15.5,
  2003,    3, 16.6,
  2004,    3, 18.7)

probs <- c(0.05, 0.25, 0.5, 0.75, 0.95)

j <- calc_term_quantiles(pq,
                         grp_col = "grp",
                         col = "val",
                         probs = probs)

pacific-hake/pacifichakemse documentation built on June 11, 2024, 4:07 a.m.