pivotSummary

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
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

long format

sumr0 %>% 
  pivot_summary()

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

long format

sumr1 %>% 
  pivot_summary(Species)

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

Group names are concatenated and pivoted.

wide format

sumr2 %>% 
   pivot_summary(matches("Spec")) 


Try the presenter package in your browser

Any scripts or data that you put into this service are public.

presenter documentation built on Nov. 18, 2021, 9:08 a.m.