library(presenter)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

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.03        3.39         1.49       0.26 
#> 2 setosa     versicolor         4.99        3.46         1.49       0.271
#> 3 setosa     virginica          5           3.43         1.41       0.211
#> 4 versicolor setosa             5.81        2.63         4.07       1.26 
#> 5 versicolor versicolor         6.01        2.82         4.28       1.35 
#> 6 versicolor virginica          5.95        2.81         4.36       1.34 
#> 7 virginica  setosa             6.6         2.91         5.56       2.07 
#> 8 virginica  versicolor         6.56        2.98         5.53       1.92 
#> 9 virginica  virginica          6.6         3.1          5.56       2.1

Group names are concatenated and pivoted.

wide format

sumr2 %>% 
   pivot_summary(matches("Spec")) 
#> # A tibble: 4 × 10
#>   column       setosa_setosa setosa_versicolor setosa_virginica versicolor_seto…
#>   <chr>                <dbl>             <dbl>            <dbl>            <dbl>
#> 1 Sepal.Length          5.03             4.99             5                 5.81
#> 2 Sepal.Width           3.39             3.46             3.43              2.63
#> 3 Petal.Length          1.49             1.49             1.41              4.07
#> 4 Petal.Width           0.26             0.271            0.211             1.26
#> # … with 5 more variables: versicolor_versicolor <dbl>,
#> #   versicolor_virginica <dbl>, virginica_setosa <dbl>,
#> #   virginica_versicolor <dbl>, virginica_virginica <dbl>