Due by 11:00pm on 9/4, submitted through Canvas

In this activity, you will explore a dataset on the results of the 2024 U.S. presidential election by county. The data can be downloaded from Canvas. There you will find a dataset (in .csv format). You should start by downloading this data file as well as the .Rmd template file to your computer, saving them both in the same folder (ideally one set up for data analysis for this course). You should then start RStudio not by clicking the application icon, but instead by double-clicking the .Rmd template which should open RStudio with the working directory for R automatically set to the location of the .Rmd file (which should also be the same location as the dataset).

Below are brief descriptions of the variables we will use in this exercise:

Question 1: Loading and Exploring the Dataset

Open RStudio and load the dataset. Since this is a comma separated value (CSV) dataset, you will use the command read.csv, which is a function specifically designed to load datasets in this format. When using this function, you will need to assign it a name. I suggest typing PrezVote <- read.csv("CountyPrezVote2024.csv")

You can now look at the first few rows of the data by typing head(PrezVote). You will probably also want to attach the dataset by typing attach(PrezVote) which will allow you to reference variables in the dataset without telling R every time to look in PrezVote for the variable.

PrezVote <- read.csv("~/Desktop/College 26-27/CountyPrezVote2024.csv")
head(PrezVote)
##   state_name county_fips    county_name gop_votes dem_votes total_votes
## 1    Alabama        1001 Autauga County     20484      7439       28190
## 2    Alabama        1003 Baldwin County     95798     24934      121808
## 3    Alabama        1005 Barbour County      5606      4158        9832
## 4    Alabama        1007    Bibb County      7572      1619        9241
## 5    Alabama        1009  Blount County     25354      2576       28115
## 6    Alabama        1011 Bullock County      1101      2983        4106
attach(PrezVote)

Question 2: Creating a new variable

Here we’ll create a variable that is the proportion of the two-party vote won by Trump. We’ll do this in several steps.

First, create a variable called votes_twoparty that gives the number of votes cast for either Harris or Trump (i.e. the sum of Harris and Trump votes). Remember this will typically be less than the total votes cast because some people vote for other candidates.

Next, using this new variable and the variable gop_votes, create a new variable called prop_rep that gives the Republican proportion of the two-party vote in each county. This is just the number of votes for Trump divided by the number of two-party votes cast.

votes_twoparty <- gop_votes + dem_votes
prop_re <- gop_votes / votes_twoparty 

Question 3: Measures of Central Tendency

Calculate the sample mean and sample median of prop_rep and make a histogram of this variable. Briefly describe what you calculated and what it tells you.

mean(prop_re)
## [1] 0.6752004
median(prop_re)
## [1] 0.71086
hist(prop_re)

#The mean is .6752004 and the median is .71086. This means the average county gave around 67.5% of it’s two party vote to Trump. While the counties in the middle of the distribution gave Trump 71% of it’s two party vote. The histogram is skewed left which means most counties gave Trump more than half of their two party votes. This is proven since the average county gave around 67.5% of it’s two party vote to Trump.

Question 4: What’s going on here?

Nationwide, Trump got around 51% of the two-party vote nationally. So how do the results in the previous question make sense? In other words, how can most counties give Trump more than 70% of their two-party vote (often much more), while Trump barely got over half of the overall two-party vote nationwide? Briefly explain this below.

Hint: Counties are different sizes. Think about what kind of counties will have a small number of voters (ones that have fewer people) and how those types of counties often vote. Think about the same thing for counties with a very large number of voters. You don’t need to do anything with R for this question, just explain your reasoning.

#There are more smaller rural counties than there are large metropolitan ones. Rural counties tend to lean more Republican which explains how most counties gave Trump more than 70% of their two-party vote. The large metropolitan counties lean more democratic and have a way bigger population than the rural ones, which explains how even if a majority of counties voted for Trump, the vote still ended up being around 51% to 49%.

Question 5: Measures of Variation

Calculate the variance and standard deviation of prop_rep. Briefly describe what you calculated and what it tells you (focus your description here on the standard deviation).

sd(prop_re)
## [1] 0.1612183
var(prop_re)
## [1] 0.02599134

#On average the average county vote share differed around 16% from the mean, which is a pretty decent spread. This deviation means the average county either gave 16% more of it’s two party votes to trump or 16% less of it’s two party votes to Trump. The variance was only around 2.5%.

Question 6: Extremes

What is the largest value for the variable prop_rep in the dataset and what is the smallest value? (Hint: the two functions max() and min() will give the maximum and minimum values, respectively, of a variable).

max(prop_re)
## [1] 0.9647266
min(prop_re)
## [1] 0.05280189

#The largest variable for prop_rep was 96% and the smallest variable was 5.2%. ### Question 7: “Red” and “Blue” Counties

Sometimes commentators discuss “red” and “blue” counties in America, dichotomizing places based on whether they voted more for the Democratic or Republican candidate. Although this is a crude simplification, it can sometimes be useful. Create a new variable called red that is TRUE for counties in which Trump won more votes than Harris and FALSE for counties in which Harris won more votes than Trump. There were no counties in which the two candidates got exactly the same number of votes so you don’t have to worry about that possibility in your coding here. (Hint: typing newvar <- var1 > var2 will create a new variable that is TRUE if var1 is greater than var2 and FALSE otherwise. Alternatively, you could also do this based on the two-party vote variable you created above.)

Then, make a table of the newly created variable and briefly describe what you learn from this table.

red <- gop_votes > dem_votes
table(red)
## red
## FALSE  TRUE 
##   475  2685

#In 2,685 counties Trump won more percent of the two party vote than Harris. In 475 counties Harris won more precent of the two party vote than Trump. This means that Trump won a majority of the county’s in the data set two party votes.

Question 8: Selecting variable entries

Make a histogram of prop_rep like you did above, but this time using only counties in Texas (ignoring counties in any other state). Briefly comment on what you see.

Hint: remember that typing x[y=="something"] will select only the entries of the variable x for observations where variable y is equal to "something". You’ll have to change x, y and "something" in your code to get the proportion of vote for Trump in only Texas counties, then use that in the hist command. It might help to make a table of the variable state_name to be sure you know what value for that variable you want to subset on.

hist(prop_re[state_name=="Texas"])

#The histograms main “center” is around 80%. Most counties are cultered between 70% and 100%. This means we can confidently say that a majority of counties in Texas voted more for Trump in the two party vote. The graph is also skewed left, which is consitent with the fact a majority of the counties voted for Trump in the two party vote.