Learn R Programming

cheese (version 0.0.3)

stratiply: Stratify a data frame, apply a function to each strata, and collect results

Description

Calls "divide" to separate a data frame into one or more stratified frames, applies a function to each strata, and optionally collects the results and returns strata to their original columns.

Usage

stratiply(
    data,
    strata,
    f,
    delimiter = "|",
    bind = FALSE,
    separate = FALSE,
    ...
)

Arguments

data

Any data.frame.

strata

See the by argument in "divide".

f

Any function that takes a data.frame as an argument.

delimiter

See the sep argument in "divide". Also used for salvaging original columns.

bind

Should the results be binded back to a single data.frame? Defaults to FALSE.

separate

If bind = TRUE, should the stratification variables be separated back to their original columns? Defaults to FALSE.

...

Additional arguments passed to f.

Value

If bind = FALSE, a named list with the results of f for each strata. Otherwise the results are binded to a single frame.

Examples

Run this code
# NOT RUN {
require(tidyverse)

#1) Return a list
heart_disease %>%
    stratiply(
        strata = c("Sex", "HeartDisease"),
        f = head
    )

#2) Unseparated strata column
heart_disease %>%
    stratiply(
        strata = c("Sex", "HeartDisease"),
        f = head,
        bind = TRUE
    )

#3) Separated strata column
heart_disease %>%
    stratiply(
        strata = c("Sex", "HeartDisease"),
        f = head,
        bind = TRUE,
        separate = TRUE
    )
    
#4) Custom function
heart_disease %>%
    stratiply(
        strata = c("Sex", "HeartDisease"),
        f = function(x) 
            x %>% 
            select_if(is.numeric) %>% 
            map(mean, na.rm = TRUE),
        bind = TRUE,
        separate = TRUE
    )    
    
#5) Regular expression
heart_disease %>%
    stratiply(
        strata = matches("^(S|H)"),
        f = function(x) 
            x %>% 
            select_if(is.numeric) %>% 
            map(mean, na.rm = TRUE),
        bind = TRUE,
        separate = TRUE
    )  
# }

Run the code above in your browser using DataLab