Learn R Programming

cheese (version 0.0.3)

stretch: Stretch one or variables over many columns by one or more keys

Description

Provides similar functionality to tidyr::spread or reshape2::dcast but allows for an unlimited number of variables to be spanned across the columns.

Usage

stretch(
    data,
    keys,
    keep = NULL,
    send = NULL,
    join = dplyr::full_join,
    .sep = "_",
    extract_keys_as_header = FALSE,
    keep_keys_in_header = TRUE,
    ...
)

Arguments

data

A data.frame object.

keys

Variables to be used as keys. See the by argument in "divide".

keep

Variables to remain as their own columns. If NULL (default), all variables except those in send are chosen, excluding the keys. Has tidyselect::vars_select capabilities.

send

Variables to send into new columns for each key. If NULL (default), all variables except those in keep are chosen, excluding the keys. Has tidyselect::vars_select capabilities.

join

Function that joins on keep across the keys. Defaults to dplyr::full_join. See ?dplyr::join for choices.

.sep

Character to separate and identify keys values over the columns. Defaults to "_".

extract_keys_as_header

Should the keys labels be returned as a separate character vector? Defaults to FALSE. Has no effect when there is only 1 send column.

keep_keys_in_header

If extract_keys_as_header = TRUE, should the keys be left in the result columns? Defaults to TRUE. Useful to set to FALSE if a call to knitr::kable follows.

...

Additional arguments to be passed to join.

Value

A wide data.frame with variables spread over the columns. If extract_keys_as_header = TRUE, the result is a two-element list with the transformed data in 1 element and the top-level header in the other. If only 1 send variable exists, the original names are removed.

Examples

Run this code
# NOT RUN {
require(tidyverse)

#Make data frame with multiple summary columns 
temp_summary <-
    
    heart_disease %>%
    group_by(
        Sex,
        HeartDisease,
        BloodSugar
    ) %>%
    summarise(
        Mean = mean(Age, na.rm = TRUE),
        SD = sd(Age, na.rm = TRUE),
        Median = median(Age, na.rm = TRUE)
    ) %>%
    ungroup() 

#1) Span summaries for each combination of Sex and BloodSugar
temp_summary %>%
    stretch(
        keys = c("Sex", "BloodSugar"),
        keep = "HeartDisease"
    )

#2) If "HeartDisease" wasn't fully crossed, use different joining to get only matching groups
temp_summary %>%
    stretch(
        keys = c("Sex", "BloodSugar"),
        keep = "HeartDisease",
        join = inner_join
    )

#3) Only send two of the summaries
temp_summary %>%
    stretch(
        keys = c("Sex", "BloodSugar"),
        keep = "HeartDisease",
        send = c("Mean", "Median")
    )
    
# }

Run the code above in your browser using DataLab