library(tidyverse)
anes <- read_csv("Data/anes.csv", show_col_types = FALSE)You Try Answers: Wrangling and Visualization
POSC 8410, Week 3
You Try (1): A Quarto Document
The task here is just to create a quarto document (.qmd file). It has three parts : a YAML header between the --- lines at the top, ordinary text written in Markdown (a typesetting language, like this paragraph), and code chunks.
The chunk below loads the tidyverse and reads the ANES data from the Data folder. It assumes that you are starting from a working directory/folder which has a Data folder which contains the .csv.
If R complains that it cannot find the file, the problem is most likely location, not code: the .qmd must be saved in your class folder, and anes.csv must be inside the Data folder within it. Quarto looks for the data relative to where the document is saved.
And the last step: Render. As long as your code contains no errors, the document should render and produce an .html file.
You Try (2): Filtering Rows
1. How many respondents voted for Trump?
filter() keeps only the rows where the condition is true, and nrow() counts them up. Here, we filter to only respondents who voted for Trump. Remember == for comparison, not =.
anes |>
filter(votechoice == "Trump") |>
nrow()[1] 2463
2,463 respondents voted for Trump.
2. How many Trump voters rate BLM above 50?
The comma inside filter() means “and”: a row is kept only if both conditions are true.
anes |>
filter(votechoice == "Trump", ft_blm > 50) |>
nrow()[1] 249
249 people, about ten percent of Trump voters.
3. How many respondents are missing a value for votechoice?
You cannot write votechoice == NA. Any comparison with a missing value returns a missing value, rather than a true or false value. is.na() is the function that actually tests for missingness.
anes |>
filter(is.na(votechoice)) |>
nrow()[1] 1572
1,572 respondents have no vote choice recorded, mostly people who did not report voting for a candidate.
You Try (3): Grouped Summaries
We built affect_gap in class with mutate(), so lets recreate it first: feeling toward Biden minus feeling toward Trump.
anes <- anes |>
mutate(affect_gap = ft_biden - ft_trump)All three parts fit in a single summarize(). (note, the British and US spellings of summarize/summarise both work. I used to live in the UK and write about British politics, so sometimes I accidentally use summarise!)
Each step adds one more line: the mean, then n(), then the standard deviation. I filter out respondents with no vote choice so the table has one row per candidate group.
anes |>
filter(!is.na(votechoice)) |>
group_by(votechoice) |>
summarize(
n = n(),
mean_gap = mean(affect_gap, na.rm = TRUE),
sd_gap = sd(affect_gap, na.rm = TRUE)
)# A tibble: 3 × 4
votechoice n mean_gap sd_gap
<chr> <int> <dbl> <dbl>
1 Biden 3267 76.3 23.2
2 Other 151 12.4 39.3
3 Trump 2463 -64.4 35.1
Biden voters average about +76, Trump voters about −64. The standard deviations answer the internal division question: Trump voters (35) are more internally divided than Biden voters (23): their gap scores are spread more widely around the group mean.
You Try (4): Plots
1–3. A histogram of ft_police, in a different color, with labels.
geom_histogram() tells R to draw a histogram, fill changes the bar color (a name like "blue" works just as well as a hex code[that’s what the #XXXXXX is called]), and labs() adds the labels. I filter out the 65 respondents missing ft_police so ggplot does not warn about dropped rows, but it’s ok if you don’t, as long as you understand what the warning tells you.
anes |>
filter(!is.na(ft_police)) |>
ggplot(aes(x = ft_police)) +
geom_histogram(bins = 40, fill = "#F56600") +
labs(x = "Feeling thermometer toward the police",
y = "Respondents",
title = "Feelings toward the police in the 2020 electorate")Note the + between layers. Pipes (|>) move data, plus signs build plots.
4. A boxplot of ft_police by votechoice.
Put the grouping variable on the x-axis and the continuous variable on the y-axis. Each box shows the median (the line), the middle 50% of the group (the box), and the tails (the whiskers).
anes |>
filter(!is.na(votechoice), !is.na(ft_police)) |>
ggplot(aes(x = votechoice, y = ft_police)) +
geom_boxplot() +
labs(x = NULL,
y = "Feeling thermometer toward the police")