knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(pollster) library(dplyr) library(knitr) library(ggplot2)
The default topline table comes with columns for response category, frequency count, percent, valid percent, and cumulative percent.
topline(df = illinois, variable = voter, weight = weight) %>% kable()
Because the output is a tibble
, it's simple to manipulate it in any way you want after creating it. Use dplyr::select
to remove columns or dplyr::filter
to remove rows. For convenience, the topline
function also provides ways to do this within the function call. For example, the remove
argument accepts a character vector of response values to be removed from the table after all statistics are calculated. This is especially useful for survey data with a "refused" category.
topline(df = illinois, variable = voter, weight = weight, remove = c("(Missing)"), pct = FALSE) %>% mutate(Frequency = prettyNum(Frequency, big.mark = ",")) %>% kable(digits = 0)
Refer to the kableExtra
package for lots of examples on how to format the appearance of these tables in either HTML or PDF latex formats. I recommend the vignettes "Create Awesome HTML Table with knitr::kable and kableExtra" and "Create Awesome PDF Table with knitr::kable and kableExtra.
topline(df = illinois, variable = voter, weight = weight) %>% ggplot(aes(Response, Percent, fill = Response)) + geom_bar(stat = "identity")
Get at topline table with the margin of error in a separate column using the moe_topline
function. By default, a z-score of 1.96 (95% confidence interval is used). Supply your own desired z-score using the zscore
argument.
moe_topline(df = illinois, variable = educ6, weight = weight)
The margin of error is calculated including the design effect of the sample weights, using the following formula:
sqrt(design effect)*zscore*sqrt((pct*(1-pct))/(n-1))*100
The design effect is calculated using the formula length(weights)*sum(weights^2)/(sum(weights)^2)
.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.