tidy_quantiles: Fast grouped sample quantiles

View source: R/tidy_quantiles.R

tidy_quantilesR Documentation

Fast grouped sample quantiles

Description

Fast grouped sample quantiles

Usage

tidy_quantiles(
  data,
  ...,
  probs = seq(0, 1, 0.25),
  type = 7,
  pivot = c("long", "wide"),
  na.rm = TRUE,
  .by = NULL,
  .cols = NULL,
  .order = group_by_order_default(data),
  .drop_groups = deprecated()
)

Arguments

data

A data frame.

...

⁠<data-masking>⁠ Variables to calculate quantiles for.

probs

numeric(n) - Quantile probabilities.

type

integer(1) - Quantile type, see ?collapse::fquantile

pivot

character(1) - Pivot result wide or long? Default is "wide".

na.rm

logical(1) Should NA values be ignored? Default is TRUE.

.by

(Optional). A selection of columns to group by for this operation. Columns are specified using tidy-select.

.cols

(Optional) alternative to ... that accepts a named character vector or numeric vector. If speed is an expensive resource, it is recommended to use this.

.order

Should the groups be returned in sorted order? If FALSE, this will return the groups in order of first appearance, and in many cases is faster.

.drop_groups

lifecycle::badge("deprecated")

Value

A data frame of sample quantiles.

Examples

library(fastplyr)
library(dplyr)
groups <- 1 * 2^(0:10)

# Normal distributed samples by group using the group value as the mean
# and sqrt(groups) as the sd

samples <- tibble(groups) |>
  reframe(x = rnorm(100, mean = groups, sd = sqrt(groups)), .by = groups) |>
  f_group_by(groups)

# Fast means and quantiles by group

quantiles <- samples |>
  tidy_quantiles(x, pivot = "wide")

means <- samples |>
  f_summarise(mean = mean(x))

means |>
  f_left_join(quantiles)

fastplyr documentation built on June 8, 2025, 11:18 a.m.