library(socsci)
## Loading required package: tidyverse
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## Registered S3 method overwritten by 'rvest':
## method from
## read_xml.response xml2
## -- Attaching packages ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.1 v purrr 0.3.2
## v tibble 2.1.1 v dplyr 0.8.0.1
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## Loading required package: rlang
##
## Attaching package: 'rlang'
## The following objects are masked from 'package:purrr':
##
## %@%, as_function, flatten, flatten_chr, flatten_dbl,
## flatten_int, flatten_lgl, flatten_raw, invoke, list_along,
## modify, prepend, splice
## Loading required package: scales
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
## Loading required package: broom
## Loading required package: glue
##
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
##
## collapse
library(labelled)
library(haven)
cces16 <- read_dta("D://cces/data/cces16.dta")
compare_fun <- function(df, var){
var <- enquo(var)
a1 <- df %>%
filter(religpew ==1) %>%
filter(religpew_protestant != 3) %>%
mutate(nn = to_factor(!! var)) %>%
ct(nn) %>%
mutate(group = "Prot - Not ND")
a2 <- df %>%
filter(religpew_protestant == 3) %>%
mutate(nn = to_factor(!! var)) %>%
ct(nn) %>%
mutate(group = "ND")
bind_rows(a1, a2)
}
cces16 %>% compare_fun(race)
## # A tibble: 16 x 4
## nn n pct group
## <fct> <int> <dbl> <chr>
## 1 White 13432 0.721 Prot - Not ND
## 2 Black 3615 0.194 Prot - Not ND
## 3 Hispanic 666 0.036 Prot - Not ND
## 4 Asian 279 0.015 Prot - Not ND
## 5 Native American 157 0.008 Prot - Not ND
## 6 Mixed 285 0.015 Prot - Not ND
## 7 Other 182 0.01 Prot - Not ND
## 8 Middle Eastern 11 0.001 Prot - Not ND
## 9 White 3518 0.717 ND
## 10 Black 720 0.147 ND
## 11 Hispanic 289 0.059 ND
## 12 Asian 106 0.022 ND
## 13 Native American 46 0.009 ND
## 14 Mixed 138 0.028 ND
## 15 Other 81 0.017 ND
## 16 Middle Eastern 8 0.002 ND
cces16 %>% compare_fun(gender)
## # A tibble: 4 x 4
## nn n pct group
## <fct> <int> <dbl> <chr>
## 1 Male 7900 0.424 Prot - Not ND
## 2 Female 10727 0.576 Prot - Not ND
## 3 Male 1977 0.403 ND
## 4 Female 2929 0.597 ND
cces16 %>% compare_fun(pid7)
## # A tibble: 20 x 4
## nn n pct group
## <fct> <int> <dbl> <chr>
## 1 Strong Democrat 4475 0.24 Prot - Not ND
## 2 Not very strong Democrat 2185 0.117 Prot - Not ND
## 3 Lean Democrat 1156 0.062 Prot - Not ND
## 4 Independent 2380 0.128 Prot - Not ND
## 5 Lean Republican 1884 0.101 Prot - Not ND
## 6 Not very strong Republican 2475 0.133 Prot - Not ND
## 7 Strong Republican 3670 0.197 Prot - Not ND
## 8 Not sure 386 0.021 Prot - Not ND
## 9 Skipped 9 0 Prot - Not ND
## 10 Not Asked 7 0 Prot - Not ND
## 11 Strong Democrat 771 0.157 ND
## 12 Not very strong Democrat 459 0.094 ND
## 13 Lean Democrat 335 0.068 ND
## 14 Independent 818 0.167 ND
## 15 Lean Republican 708 0.144 ND
## 16 Not very strong Republican 729 0.149 ND
## 17 Strong Republican 982 0.2 ND
## 18 Not sure 100 0.02 ND
## 19 Skipped 2 0 ND
## 20 Not Asked 2 0 ND
cces16 %>% compare_fun(pew_churatd)
## # A tibble: 16 x 4
## nn n pct group
## <fct> <int> <dbl> <chr>
## 1 More than once a week 2709 0.145 Prot - Not ND
## 2 Once a week 4631 0.249 Prot - Not ND
## 3 Once or twice a month 2030 0.109 Prot - Not ND
## 4 A few times a year 3070 0.165 Prot - Not ND
## 5 Seldom 4233 0.227 Prot - Not ND
## 6 Never 1767 0.095 Prot - Not ND
## 7 Don't know 175 0.009 Prot - Not ND
## 8 Skipped 12 0.001 Prot - Not ND
## 9 More than once a week 840 0.171 ND
## 10 Once a week 1519 0.31 ND
## 11 Once or twice a month 497 0.101 ND
## 12 A few times a year 690 0.141 ND
## 13 Seldom 907 0.185 ND
## 14 Never 409 0.083 ND
## 15 Don't know 43 0.009 ND
## 16 Skipped 1 0 ND
cces16 %>% compare_fun(educ)
## # A tibble: 12 x 4
## nn n pct group
## <fct> <int> <dbl> <chr>
## 1 No HS 556 0.03 Prot - Not ND
## 2 High school graduate 5154 0.277 Prot - Not ND
## 3 Some college 4595 0.247 Prot - Not ND
## 4 2-year 2222 0.119 Prot - Not ND
## 5 4-year 3869 0.208 Prot - Not ND
## 6 Post-grad 2231 0.12 Prot - Not ND
## 7 No HS 93 0.019 ND
## 8 High school graduate 1024 0.209 ND
## 9 Some college 1383 0.282 ND
## 10 2-year 642 0.131 ND
## 11 4-year 1186 0.242 ND
## 12 Post-grad 578 0.118 ND