pivot_helper: A function that pretty much let's you do anything in the...

View source: R/pivot_helper.R

pivot_helperR Documentation

A function that pretty much let's you do anything in the tidypivot space, is carefully crafted, and is adapted to make the other functions easy to use

Description

A function that pretty much let's you do anything in the tidypivot space, is carefully crafted, and is adapted to make the other functions easy to use

Usage

pivot_helper(
  data,
  rows = NULL,
  cols = NULL,
  value = NULL,
  wt = NULL,
  fun = NULL,
  prop = NULL,
  within = NULL,
  withinfun = NULL,
  pivot = NULL,
  wrap = NULL,
  totals_within = NULL
)

Arguments

data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

rows

a character vector of items

cols

a character vector of items

pivot

logical: should wide table be returned - col categories as columns (TRUE), or left long and tidy (FALSE)?

Examples

tidy_titanic %>% pivot_helper(rows = sex, cols = survived, fun = length) # pivot_count
flat_titanic %>% pivot_helper(rows = sex, value = freq, fun = mean) # pivot_calc
flat_titanic %>% pivot_helper(rows = sex, value = freq, fun = sum) # pivot_count (weighted sum)
nar <- function(x) return(NA)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = nar); #pivot_null
sample1 <- function(x) sample(x, 1)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = sample1, value = freq); #pivot_sample1
samplen <- function(x, n) paste(sample(x, 5, replace = F), collapse = ", ")
# flat_titanic %>% pivot_helper(rows = sex, cols = survived, fun = samplen, value = freq); #pivot_samplen
paste_collapse <- function(x) paste (x, collapse = ", ")
flat_titanic %>% pivot_helper(rows = sex, fun = paste_collapse, value = freq) # pivot_list
flat_titanic %>% pivot_helper(rows = sex, value = freq, prop = TRUE) # pivot_prop
flat_titanic %>% pivot_helper(rows = sex, cols = survived, value = freq, prop = TRUE)
flat_titanic %>% pivot_helper(rows = sex, cols = survived, value = freq, prop = TRUE, within = sex)

EvaMaeRey/tidypivot documentation built on Feb. 27, 2025, 4:04 a.m.