This is the code used to complete the midterm exam.
Task 1: Using the code I have provided above, install and load the tidyverse package, and create the Inc2022 and Inc2017 data frames. Accompany the code with a brief explanation of how the code works.
Response:
if(!require(tidyverse))install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyverse)
Inc2022 <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/Inc2022.csv")
Inc2017 <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/Inc2017.csv")
this code opens up “tidyverse”. it then use read.csv() function to show us income data from 2022 and 2017.
Task 2: Write R code that uses the left_join() function to merge the Inc2022 and Inc2017 data frames into a new data frame called Income, then shows the first 10 rows of the Income data frame. Explain which data frame was your “to” data frame, which was your “from” data frame, and which variables served as your “key column” variables.
Response:
head(Inc2017,10)
## GEOID HHInc2017
## 1 4702190022 44091
## 2 4702190212 55247
## 3 4702190402 73802
## 4 4702190592 49731
## 5 4702190782 60793
## 6 4702190972 66750
## 7 4703790038 56258
## 8 4703790228 32487
## 9 4703790418 49402
## 10 4703790608 79381
head(Inc2022,10)
## GEOID District County HHInc2022 Significance
## 1 4702190022 District 1 Cheatham County 59741 Significant
## 2 4702190212 District 2 Cheatham County 73634 Significant
## 3 4702190402 District 3 Cheatham County 89861 Nonsignificant
## 4 4702190592 District 4 Cheatham County 73293 Significant
## 5 4702190782 District 5 Cheatham County 78380 Significant
## 6 4702190972 District 6 Cheatham County 92305 Significant
## 7 4703790038 District 1 Davidson County 75038 Significant
## 8 4703790228 District 2 Davidson County 54346 Significant
## 9 4703790418 District 3 Davidson County 67953 Significant
## 10 4703790608 District 4 Davidson County 106260 Significant
Income <- left_join(Inc2022,
Inc2017,
by = join_by(GEOID == GEOID))
The left_join function joins these two data frames (Inc2022, Inc2017) into a data frame called “Income”. Our “from” data frame is Inc2017, and our “to” data frame is Inc2022. GEOID serves as the function to combine the data sets. The head functions display 10 rows of each.
Task 3: Write R code that adds a variable called “Change” to the Income data frame showing the difference between each district’s 2022 and 2017 income estimates and displays the first 10 rows of the Income. Explain which function you used to create the “Change” variable.
Response:
Income <- Income %>%
mutate(Change = HHInc2022 - HHInc2017)
the mutate function adds new variables to a data set. here we added a variable called change that shows the difference between incomes in 2017 to incomes in 2022.
Task 4: Write code that adds a variable called “Level” to the Income data frame indicating whether each district’s Inc2022 figure is $100,000 or more, or less than $100,000. The “Level” variable should read, “$100k+” for districts where Inc2022 is 100,000 or more, and “<$100k” for districts where Inc2022 is less than 100,000. Explain which functions you used to create the “Level” variable.
Response:
Income <- Income %>%
mutate(Level = case_when(HHInc2022 < 100000 ~ "<$100K",
HHInc2022 == 100000 ~ "No change",
HHInc2022 > 100000 ~ "$100k+",
.default = "Error"))
mutate adds a new variable called “level” in the case where household incomes rise or fall to $100k.
Task 5: Create a data frame called “LevelbyCounty” that aggregates the data to show how many districts in each county are in the “Level” variable’s “$100k+” and “<$100k” categories. Show the “LevelbyCounty” data frame’s first 10 rows. Explain the functions you used. Your code’s output should include a table that looks like this one, except that every county should have a value in the “<$100k” and “$100k+” columns, not just Cheatham County. I left the Cheatham County figures visible to help you make sure you set the process up correctly.
Response:
LevelByCounty <- Income
LevelByCounty <- group_by(LevelByCounty, County, Level)
LevelByCounty<- summarize(LevelByCounty,Count= n ())
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
LevelByCounty<- pivot_wider(LevelByCounty, names_from = Level, values_from = Count)
we created a new data frame called LevelByCounty that mirrors our Income data frame. the we instructed R to group them together based on if they are above or below the $100k value. Count gives a number of districts that fall into those categories. you may also use the head function to display just ten rows.
Task 6: Write code that creates a “RichDistricts” data frame containing rows for only those districts in the “Level” variable’s “$100k+” category and shows the first 10 rows of the “RichDistricts” data frame. Explain which function you used.
Response:
RichDistricts <- Income
RichDistricts <- Income %>%
filter(Level=="$100k+")
head(RichDistricts,10)
## GEOID District County HHInc2022 Significance HHInc2017
## 1 4703790608 District 4 Davidson County 106260 Significant 79381
## 2 4703794218 District 23 Davidson County 117474 Significant 86768
## 3 4703794408 District 24 Davidson County 110739 Significant 79959
## 4 4703794598 District 25 Davidson County 120206 Significant 90998
## 5 4703796460 District 34 Davidson County 161370 Significant 128421
## 6 4703796650 District 35 Davidson County 117294 Significant 99666
## 7 4714791098 District 6 Robertson County 106058 Significant 65179
## 8 4714991480 District 8 Rutherford County 102862 Significant 75875
## 9 4714991670 District 9 Rutherford County 103006 Significant 77481
## 10 4714992620 District 14 Rutherford County 105750 Nonsignificant 95708
## Change Level
## 1 26879 $100k+
## 2 30706 $100k+
## 3 30780 $100k+
## 4 29208 $100k+
## 5 32949 $100k+
## 6 17628 $100k+
## 7 40879 $100k+
## 8 26987 $100k+
## 9 25525 $100k+
## 10 10042 $100k+
this code creates a data frame called “richdistricts” that only shows districts in the $100k+ level. head function displays ten rows of this.
Task 7: Write code that sorts the “RichDistricts” data frame by the “HHInc2022” variable, in descending order (biggest value at the top), and displays the first 10 rows of the data frame. Explain which function you used.
Response:
RichDistricts <- RichDistricts %>%
arrange(desc(HHInc2022))
head(RichDistricts,10)
## GEOID District County HHInc2022 Significance HHInc2017
## 1 4718791328 District 7 Williamson County 181709 Significant 150106
## 2 4718791138 District 6 Williamson County 178665 Significant 154149
## 3 4703796460 District 34 Davidson County 161370 Significant 128421
## 4 4718790948 District 5 Williamson County 159737 Significant 121622
## 5 4718791708 District 9 Williamson County 144924 Significant 117526
## 6 4718993040 District 16 Wilson County 125785 Significant 74528
## 7 4718990570 District 3 Wilson County 125324 Significant 80577
## 8 4718790758 District 4 Williamson County 124237 Nonsignificant 127552
## 9 4718790378 District 2 Williamson County 123609 Significant 101687
## 10 4718990380 District 2 Wilson County 120302 Significant 98846
## Change Level
## 1 31603 $100k+
## 2 24516 $100k+
## 3 32949 $100k+
## 4 38115 $100k+
## 5 27398 $100k+
## 6 51257 $100k+
## 7 44747 $100k+
## 8 -3315 $100k+
## 9 21922 $100k+
## 10 21456 $100k+
this code sorts the “Rich Districts” data frame based on 2022 income data using the arrange function. desc puts them in a descending order. head shows 10 rows.
Task 8: Look back over the results you got and summarize, in a few sentences, at least one interesting pattern you discovered pertaining to how income levels vary across districts and/or counties in the Nashville area.
Response: overall i observed a pattern of williamson and wilson county having a majority “rich districts”. whereas counties like rutherford and davidson having a majority of non rich counties. this could be explained by who can afford to live in these respective counties. rutherford is full of students and workers who commute to nashville but do not live there. we could analyze wages in each county to see how much of a discrepancy there is between household incomes. the county that saw the most growth in income from 2017 <- 2022 was wilson county. they have two districts with over $50k in growth. overall, the Level by county data gives valuable data on what counties are experiencing job growth and income raises.