Skip to content

Instantly share code, notes, and snippets.

@mine-cetinkaya-rundel
Created February 5, 2020 15:11
Show Gist options
  • Save mine-cetinkaya-rundel/758cbde5a6e6a5d3adeb6b089e91664c to your computer and use it in GitHub Desktop.
Save mine-cetinkaya-rundel/758cbde5a6e6a5d3adeb6b089e91664c to your computer and use it in GitHub Desktop.
Example using tidyr::uncount()
# data source ------------------------------------------------------------------
# Are federal laws on gun ownership too strict? Not strict enough? Or just about right?
# http://www.surveyusa.com/client/PollReport.aspx?g=e2893631-b41f-40e1-a049-86e0849670ce
# load packages ----------------------------------------------------------------
library(tidyverse)
# frequency table --------------------------------------------------------------
freq_table <- tribble(
~response, ~n,
"too strict", 99,
"not strict enough", 507,
"just about right", 151,
"not sure", 42
)
freq_table
#> # A tibble: 4 x 2
#> response n
#> <chr> <dbl>
#> 1 too strict 99
#> 2 not strict enough 507
#> 3 just about right 151
#> 4 not sure 42
# frequency table to data frame of individual observations
df <- uncount(freq_table, weights = n)
df
#> # A tibble: 799 x 1
#> response
#> <chr>
#> 1 too strict
#> 2 too strict
#> 3 too strict
#> 4 too strict
#> 5 too strict
#> 6 too strict
#> 7 too strict
#> 8 too strict
#> 9 too strict
#> 10 too strict
#> # … with 789 more rows
# back to frequency table
df %>%
count(response)
#> # A tibble: 4 x 2
#> response n
#> <chr> <int>
#> 1 just about right 151
#> 2 not strict enough 507
#> 3 not sure 42
#> 4 too strict 99
# contingency table ------------------------------------------------------------
cont_table <- tribble(
~response, ~male, ~female,
"too strict", 65, 34,
"not strict enough", 216, 291,
"just about right", 100, 51,
"not sure", 12, 30
)
cont_table
#> # A tibble: 4 x 3
#> response male female
#> <chr> <dbl> <dbl>
#> 1 too strict 65 34
#> 2 not strict enough 216 291
#> 3 just about right 100 51
#> 4 not sure 12 30
# contingency table to data frame of individual observations
df <- cont_table %>%
pivot_longer(cols = c(male, female), names_to = "gender") %>%
uncount(weights = value)
df
#> # A tibble: 799 x 2
#> response gender
#> <chr> <chr>
#> 1 too strict male
#> 2 too strict male
#> 3 too strict male
#> 4 too strict male
#> 5 too strict male
#> 6 too strict male
#> 7 too strict male
#> 8 too strict male
#> 9 too strict male
#> 10 too strict male
#> # … with 789 more rows
# back to contingency table
df %>%
count(response, gender) %>%
pivot_wider(names_from = gender, values_from = n)
#> # A tibble: 4 x 3
#> response female male
#> <chr> <int> <int>
#> 1 just about right 51 100
#> 2 not strict enough 291 216
#> 3 not sure 30 12
#> 4 too strict 34 65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment