5: Tidy Data
Data Wrangling and Visualization
Before attempting this assignment, be sure to install the tidyverse package into you local R installation.
Data Work
Use the Cooperative Congressional Election Study/Cooperative Election Study (CCES/CES) Cumulative file.
Use only the 2024 data. Recode the 5 category ideology variable to a three category variable: liberal, moderate, conservative.
Recode the presidential vote variable to look just at two party vote (in other words, recode vote so that respondents who reported voting for candidates other than Trump or Harris are βmissingβ).
Create a table. What proportion of respondents who are liberal voted for Trump? What proportion of respondents who are conservative voted for Harris?
Find the variable that refers to whether people own or rent their home. Recode the variable so that βotherβ is missing.
Create a table. What portion of people who own their homes voted for Trump? Harris? What portion of people who rented voted for Trump? Harris?
Finally, locate the education variable. Recode the variable so that those with a high school degree or less are in one category, those with some college but less than a four year degree are in a second, those with a college degree are in a third, and those with a post-grad degree are in a fourth.
Create a table. What does education tell us about vote in the 2024 election?
Submission
- Use a quarto file and a GitHub repository with this assignment. You may use a separate R file for analysis (my recommendation), or embed your R code within Quarto.
- Render your quarto file to .html and .pdf.
- Push your completed assignment, including your .html and .pdf files, to your GitHub repo.
- Either:
- Ensure the repo is public, or
- If it is private, share the repo with me (Iβm
@jacklreillyon GitHub).
- On Blackboard, place a link to the repo so that I can click straight through to it.
You can accomplish what this assignment asks in a lot of ways, but things probably ultiamtely go a little smoother for you if you read the
.dtafile, rather than the native R data file, and useread_dta()(which reads the data in as a tibble) rather thanread.dta()(from the foreign package).To use only the 2024 CES data, enter this code after you read in the whole CES:
ces2024 <- ces |> filter(year == 2024)Then use that new
ces2024object for the rest of the analysis, which will contain only 2024 data.Presuming you read your data in as a tibble, calling a variable (say,
ideo5) will show you the levels of the variable if you just look at it:ces2024Alternately, you can also look only at labels and their associated numbers like this:
attr(ces2024$ideo5, "labels")