Learn R Programming

fastplyr (version 0.2.0)

tidy_quantiles: Fast grouped sample quantiles

Description

Fast grouped sample quantiles

Usage

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

Value

A data frame of sample quantiles.

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.

.drop_groups

logical(1) Should groups be dropped after calculation? Default is TRUE.

Examples

Run this code
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)

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

means %>%
  f_left_join(quantiles)

Run the code above in your browser using DataLab