knitr::opts_chunk$set( warning = FALSE, message = FALSE, comment = FALSE, echo = TRUE ) library(tidyverse) library(ussc) library(knitr) library(kableExtra) load("survey_long_data.rdata")
The intended purpose for this write-up is to explain the two new weighted prop.table functions in ussc
.
The first, ussc::prop_survey_question()
, calculates the weighted proportion for specified questions across both countries and spreads the results so that the resulting data frame is wide (i.e. corresponding US and Australian proportions for the question and answer are side-by-side). It works as follows:
f1 <- survey_data %>% prop_survey_question(questions = starts_with("stance_on_tarrifs")) %>% select(-description_au) %>% kable("html", digits = 0, booktabs = TRUE) f1
The sample group has already been specified for you. You do need to make note of the particular questions you want to select.
Keep in mind that the answer column has been coerced to a character variable. To turn it back into a factor, run the following function relevel_survey_answer
:
survey_data %>% prop_survey_question(questions = starts_with("stance_on_tarrifs")) %>% relevel_survey_answer(c("Good for the American economy", "Neither good nor bad for the American economy", "Bad for the American economy")) %>% select(-description_au) %>% kable("html", digits = 0, booktabs = TRUE)
Your data frame will reorder based on the factor levels in the answer column.
The second function, ussc::prop_grouped_survey_question()
allows the user to group by two or more variables, like so:
f2 <- survey_data %>% prop_grouped_survey_question(questions = starts_with("sexual_harrassment"), partyid) %>% select(-description_au) %>% rename_all(~sub('Australia_|(United States_)', '', .x)) %>% rename("AU other/none" = `Other or none`) knitr::kable(f2, digits = 0, booktabs = TRUE)
You can group by multiple variables, but keep in mind that the tables will get very wide and we will need to fit our output on a standard A4 paper.
Both functions return the description for the Australian survey and the American survey. The questions were often the exact same, so you can simply filter out a description column.
The column names will return information about the country and grouping variables. These, of course, should be cleaned up. I leave this up to the user.
Of course, you can pass other functions to the returned object. A pretty kable table can be pulled together quite quickly, e.g.
t <- f2 %>% select(-description_us, -`AU other/none`) %>% rename(" " = "answer") %>% knitr::kable("html", booktabs = T, digits = 0) %>% kableExtra::add_header_above(c(" " = 1, "Australia" = 3, "United States" = 3), bold = T) %>% kableExtra::group_rows(index = c("Men getting away with committing sexual harassment and assault" = 3, "Women making false claims about sexual harassment or assault" = 3, "Women not being believed about sexual harassment and assault" = 3)) t
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.