Learn R Programming

fastplyr (version 0.5.1)

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("long", "wide"),
  na.rm = TRUE,
  .by = NULL,
  .cols = NULL,
  .order = df_group_by_order_default(data),
  .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.

.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

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, pivot = "wide")

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

means %>%
  f_left_join(quantiles)

Run the code above in your browser using DataLab