# pivotSummary

    library(presenter)
    library(dplyr)

# Pivot summary

Transpose a tibble of summary statistics in tidy format. Convenient function for transposing the output of dplyr”s `group_by` and `summarize` operation.

## 0 groups

Transpose a 1 row numerical summary:

wide format

    iris %>%
      summarize(across(where(is.numeric), mean), .groups = "drop") -> sumr0

    sumr0
    #>   Sepal.Length Sepal.Width Petal.Length Petal.Width
    #> 1     5.843333    3.057333        3.758    1.199333

long format

    sumr0 %>%
      pivot_summary()
    #> # A tibble: 4 × 2
    #>   column          V1
    #>   <chr>        <dbl>
    #> 1 Sepal.Length  5.84
    #> 2 Sepal.Width   3.06
    #> 3 Petal.Length  3.76
    #> 4 Petal.Width   1.20

## 1 group

A grouped summary can be transposed by providing the name of the group column.

wide format

    iris %>%
      group_by(Species) %>%
      summarize(across(where(is.numeric), mean), .groups = "drop") -> sumr1

    sumr1
    #> # A tibble: 3 × 5
    #>   Species    Sepal.Length Sepal.Width Petal.Length Petal.Width
    #>   <fct>             <dbl>       <dbl>        <dbl>       <dbl>
    #> 1 setosa             5.01        3.43         1.46       0.246
    #> 2 versicolor         5.94        2.77         4.26       1.33
    #> 3 virginica          6.59        2.97         5.55       2.03

long format

    sumr1 %>%
      pivot_summary(Species)
    #> # A tibble: 4 × 4
    #>   column       setosa versicolor virginica
    #>   <chr>         <dbl>      <dbl>     <dbl>
    #> 1 Sepal.Length  5.01        5.94      6.59
    #> 2 Sepal.Width   3.43        2.77      2.97
    #> 3 Petal.Length  1.46        4.26      5.55
    #> 4 Petal.Width   0.246       1.33      2.03

## 2 groups

Supports transposing numerical summaries with multiple groups using tidyselect.

long format

    iris %>%
      mutate(Species1 = sample(Species)) %>%
      group_by(Species, Species1) %>%
      summarize(across(where(is.numeric), mean), .groups = "drop") -> sumr2

    sumr2
    #> # A tibble: 9 × 6
    #>   Species    Species1   Sepal.Length Sepal.Width Petal.Length Petal.Width
    #>   <fct>      <fct>             <dbl>       <dbl>        <dbl>       <dbl>
    #> 1 setosa     setosa             5.13        3.61         1.46       0.269
    #> 2 setosa     versicolor         4.99        3.34         1.46       0.217
    #> 3 setosa     virginica          4.93        3.38         1.47       0.258
    #> 4 versicolor setosa             5.92        2.78         4.34       1.32
    #> 5 versicolor versicolor         5.94        2.78         4.23       1.35
    #> 6 versicolor virginica          5.95        2.75         4.2        1.3
    #> 7 virginica  setosa             6.73        3.11         5.73       2.08
    #> 8 virginica  versicolor         6.62        2.92         5.44       1.95
    #> 9 virginica  virginica          6.42        2.87         5.44       2.02

Group names are concatenated and pivoted.

wide format

    sumr2 %>%
       pivot_summary(matches("Spec"))
    #> # A tibble: 4 × 10
    #>   column setos…¹ setos…² setos…³ versi…⁴ versi…⁵ versi…⁶ virgi…⁷ virgi…⁸ virgi…⁹
    #>   <chr>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
    #> 1 Sepal…   5.13    4.99    4.93     5.92    5.94    5.95    6.73    6.62    6.42
    #> 2 Sepal…   3.61    3.34    3.38     2.78    2.78    2.75    3.11    2.92    2.87
    #> 3 Petal…   1.46    1.46    1.47     4.34    4.23    4.2     5.73    5.44    5.44
    #> 4 Petal…   0.269   0.217   0.258    1.32    1.35    1.3     2.08    1.95    2.02
    #> # … with abbreviated variable names ¹​setosa_setosa, ²​setosa_versicolor,
    #> #   ³​setosa_virginica, ⁴​versicolor_setosa, ⁵​versicolor_versicolor,
    #> #   ⁶​versicolor_virginica, ⁷​virginica_setosa, ⁸​virginica_versicolor,
    #> #   ⁹​virginica_virginica