Answered step by step
Verified Expert Solution
Question
1 Approved Answer
: Election Returns- r language needs to be used For the first question, were going to be working with 2012 and 2016 election returns. This
: Election Returns- r language needs to be used
For the first question, were going to be working with 2012 and 2016 election returns. This question focuses on data importing, data cleaning, and mapping.
- Comprehensive election returns have been compiled by Dave Leip, and are available on Canvas or via the Penn library at this link. In part because this data was designed for Excel, theres a bit of data cleaning we need to do before we get to work with the actual numbers.
- Each years Presidential election results data is included in a separate Excel file. Download the appropriate 2012 and 2016 spreadsheets, and import the county-level returns (sheet 3) into R for both 2012 and 2016. Read in the data, and then to make our lives easier, use the clean_names command from the janitor package to cleanup the name of each column.
- Lets do some data cleaning. We only want a subset of everything Leip includes: we want to know the state and county name, the percentage won by Obama (Clinton), Romney (Trump), and other candidates (just the Other columns, you dont have to pull in every other candidate individually), the total vote count, and the FIPS code. Use the tools youve learned to reduce both years data down to just those variables.
- Next, were going to remove all rows that dont contain any data (he inserts these so that theyre easier to read in Excel). Use the Total Vote column to assess if a row has data or not, and remove the ones that are missing values.
- Next, rename the columns to something substantive so that their labels make sense. That is, call them things like county rather than x1.
- Merge the 2012 and 2016 data together so that you have one set of electoral data with one line of data for each county.
- Now that we have our election returns clean, lets merge it with our mapping data. Import the county-level mapping dataset that we worked with in previous modules from the package mapdata. Next, join this dataset with the county-level FIPS codes in R and then join it to our electoral data.
- Now create two maps that show the percentage of the vote that Hillary Clinton and Barack Obama won in 2016 and 2012, respectively, using a chloropleth map with a color scale of red->purple->blue, similar to Homework 6.
- Create a new column showing the change in Democratic vote percentage from 2012 to 2016 (just the 2016 Democratic vote percentage minus the 2012 Democratic vote percentage) and map this change. Make areas with a decreased Democratic vote share red and those with an increased democratic vote share blue.
- Next, were going to attempt to explain this change. To do this, download and import the education dataset from Canvas. Join this with the presidential returns.
- Finally, plot the relationship between the Democratic shift 2012-2016 and the percentage of people in a county who have attained a bachelors degree (pct.bach in the education data). Put the Democratic shift on the y axis. Explain what is happening in this plot.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started