Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Created December 30, 2015 17:14
Show Gist options
  • Save dmarcelinobr/ddfde32ff72cd38a109c to your computer and use it in GitHub Desktop.
Save dmarcelinobr/ddfde32ff72cd38a109c 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