Skip to content

Instantly share code, notes, and snippets.

@trinker
Created December 18, 2015 22:32
Show Gist options
  • Save trinker/184cc9f0624bad85de10 to your computer and use it in GitHub Desktop.
Save trinker/184cc9f0624bad85de10 to your computer and use it in GitHub Desktop.
crosstabs with tidyr and dplyr
if (!require("pacman")) install.packages("pacman")
pacman::p_load(dplyr, tidyr, wakefield)
set.seed(10)
dat <- r_data_frame(n = 10000,
race,
age,
sex
)
dat %>%
count(Race, Age, Sex) %>%
spread(Age, n, fill =0)
dat %>%
count(Race, Age, Sex) %>%
spread(Race, n, fill =0)
dat %>%
count(Race, Age, Sex) %>%
unite(Race_Sex, Race, Sex) %>%
spread(Race_Sex, n, fill =0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment