stretch {cheese}R Documentation

Span keys and values across the columns

Description

Pivot one or more values across the columns by one or more keys

Usage

stretch(
    data,
    key,
    value,
    sep = "_"
)

Arguments

data

A data.frame.

key

A vector of quoted/unquoted columns, positions, and/or tidyselect::select_helpers whose values will become the column name(s).

value

A vector of quoted/unquoted columns, positions, and/or tidyselect::select_helpers whose values will be spread across the columns.

sep

String to separate keys/values by in the resulting column names. Defaults to "_". Only used when there are more than one keys/values.

Details

In the case of multiple value's, the labels are always appended to the end of the resulting columns.

Value

A tibble::tibble

Author(s)

Alex Zajichek

Examples


#Make a summary table
set.seed(123)
data <- 
  heart_disease %>%
  dplyr::group_by(
    Sex,
    BloodSugar,
    HeartDisease
  ) %>%
  dplyr::summarise(
    Mean = mean(Age),
    SD = sd(Age),
    .groups = "drop"
  ) %>%
  dplyr::mutate(
    Random =
      rbinom(nrow(.), size = 1, prob = .5) %>%
      factor
  )

data %>%
  stretch(
    key = c(BloodSugar, HeartDisease),
    value = c(Mean, SD, Random)
  )

data %>%
  stretch(
    key = where(is.factor),
    value = where(is.numeric)
  )

data %>%
  stretch(
    key = c(where(is.factor), where(is.logical)),
    value = where(is.numeric)
  )


[Package cheese version 0.1.2 Index]