Split up columns into groups and apply a function to combinations of those columns with control over whether each group is entered as a single data.frame or individual vector's.

dish(
    data,
    f,
    left,
    right,
    each_left = TRUE,
    each_right = TRUE,
    ...
)

Arguments

data

A data.frame.

f

A function that takes a vector and/or data.frame in the first two arguments.

left

A vector of quoted/unquoted columns, positions, and/or tidyselect::select_helpers to be evaluated in the first argument of f.

right

A vector of quoted/unquoted columns, positions, and/or tidyselect::select_helpers to be evaluated in the second argument of f.

each_left

Should each left variable be indivdually evaluated in f? Defaults to TRUE. If FALSE, left columns are entered into f as a single data.frame.

each_right

Should each right variable be individually evaluated in f? Defaults to TRUE. If FALSE, right columns are entered into f as a single data.frame.

...

Additional arguments to be passed to f.

Value

A list

Author

Alex Zajichek

Examples

#All variables on both sides
heart_disease %>%
    dplyr::select(
        where(is.numeric)
    ) %>%
    dish(
        f = cor
    )
#> $Age
#> $Age$Age
#> [1] 1
#> 
#> $Age$BP
#> [1] 0.2849459
#> 
#> $Age$Cholesterol
#> [1] 0.2089503
#> 
#> $Age$MaximumHR
#> [1] -0.3938058
#> 
#> 
#> $BP
#> $BP$Age
#> [1] 0.2849459
#> 
#> $BP$BP
#> [1] 1
#> 
#> $BP$Cholesterol
#> [1] 0.1301201
#> 
#> $BP$MaximumHR
#> [1] -0.04535088
#> 
#> 
#> $Cholesterol
#> $Cholesterol$Age
#> [1] 0.2089503
#> 
#> $Cholesterol$BP
#> [1] 0.1301201
#> 
#> $Cholesterol$Cholesterol
#> [1] 1
#> 
#> $Cholesterol$MaximumHR
#> [1] -0.003431832
#> 
#> 
#> $MaximumHR
#> $MaximumHR$Age
#> [1] -0.3938058
#> 
#> $MaximumHR$BP
#> [1] -0.04535088
#> 
#> $MaximumHR$Cholesterol
#> [1] -0.003431832
#> 
#> $MaximumHR$MaximumHR
#> [1] 1
#> 
#> 

#Simple regression of each numeric variable on each other variable
heart_disease %>%
    dish(
        f =
            function(y, x) {
                mod <- lm(y ~ x)
                tibble::tibble(
                    Parameter = names(mod$coef),
                    Estimate = mod$coef
                )
            },
        left = where(is.numeric)
    ) %>%
    
    #Bind rows together
    fasten(
        into = c("Outcome", "Predictor")
    )
#> # A tibble: 48 × 4
#>    Outcome Predictor             Parameter         Estimate
#>    <chr>   <chr>                 <chr>                <dbl>
#>  1 Age     Sex                   (Intercept)         55.7  
#>  2 Age     Sex                   xMale               -1.89 
#>  3 Age     ChestPain             (Intercept)         55.9  
#>  4 Age     ChestPain             xAtypical angina    -4.51 
#>  5 Age     ChestPain             xNon-anginal pain   -2.17 
#>  6 Age     ChestPain             xAsymptomatic       -0.147
#>  7 Age     BloodSugar            (Intercept)         54.0  
#>  8 Age     BloodSugar            xTRUE                3.01 
#>  9 Age     ExerciseInducedAngina (Intercept)         53.9  
#> 10 Age     ExerciseInducedAngina xYes                 1.76 
#> # … with 38 more rows

#Multiple regression of each numeric variable on all others simultaneously
heart_disease %>%
    dish(
        f =
            function(y, x) {
                mod <- lm(y ~ ., data = x)
                tibble::tibble(
                    Parameter = names(mod$coef),
                    Estimate = mod$coef
                )
            },
        left = where(is.numeric),
        each_right = FALSE
    ) %>%
    
    #Bind rows together
    fasten(
        into = "Outcome"
    )
#> # A tibble: 32 × 3
#>    Outcome Parameter                 Estimate
#>    <chr>   <chr>                        <dbl>
#>  1 Age     (Intercept)                 56.8  
#>  2 Age     SexMale                     -3.53 
#>  3 Age     ChestPainAtypical angina    -4.28 
#>  4 Age     ChestPainNon-anginal pain   -2.51 
#>  5 Age     ChestPainAsymptomatic       -2.08 
#>  6 Age     BloodSugarTRUE               2.90 
#>  7 Age     ExerciseInducedAnginaYes    -0.294
#>  8 Age     HeartDiseaseYes              4.59 
#>  9 BP      (Intercept)                141.   
#> 10 BP      SexMale                     -5.43 
#> # … with 22 more rows