TASK #1 Response: The top code is an install command that is an add-on, in case the tidyverse has not been put in. The other sets of code import the data and download them into data frames, naming them Inc2022 and Inc2017
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")
Task 2 response: The Inc2022 data frame is used as the “to” code while the Inc2017 data frame is used as the “from” code. The “key column” variable used from both data frames was the GEOID.
Income <- left_join(Inc2022,
Inc2017,
by = join_by(GEOID == GEOID))
head(Income, 10)
## GEOID District County HHInc2022 Significance HHInc2017
## 1 4702190022 District 1 Cheatham County 59741 Significant 44091
## 2 4702190212 District 2 Cheatham County 73634 Significant 55247
## 3 4702190402 District 3 Cheatham County 89861 Nonsignificant 73802
## 4 4702190592 District 4 Cheatham County 73293 Significant 49731
## 5 4702190782 District 5 Cheatham County 78380 Significant 60793
## 6 4702190972 District 6 Cheatham County 92305 Significant 66750
## 7 4703790038 District 1 Davidson County 75038 Significant 56258
## 8 4703790228 District 2 Davidson County 54346 Significant 32487
## 9 4703790418 District 3 Davidson County 67953 Significant 49402
## 10 4703790608 District 4 Davidson County 106260 Significant 79381
I used the mutate function since we are looking for a difference in the two income data sets between the years, leading to the change variable.
Income <- Income %>%
mutate(Change = HHInc2022 - HHInc2017)
head(Income, 10)
## GEOID District County HHInc2022 Significance HHInc2017
## 1 4702190022 District 1 Cheatham County 59741 Significant 44091
## 2 4702190212 District 2 Cheatham County 73634 Significant 55247
## 3 4702190402 District 3 Cheatham County 89861 Nonsignificant 73802
## 4 4702190592 District 4 Cheatham County 73293 Significant 49731
## 5 4702190782 District 5 Cheatham County 78380 Significant 60793
## 6 4702190972 District 6 Cheatham County 92305 Significant 66750
## 7 4703790038 District 1 Davidson County 75038 Significant 56258
## 8 4703790228 District 2 Davidson County 54346 Significant 32487
## 9 4703790418 District 3 Davidson County 67953 Significant 49402
## 10 4703790608 District 4 Davidson County 106260 Significant 79381
## Change
## 1 15650
## 2 18387
## 3 16059
## 4 23562
## 5 17587
## 6 25555
## 7 18780
## 8 21859
## 9 18551
## 10 26879
I used the mutate and case_when functions, with the mutate function creating the level variable, and the case_when function to make a scenario where the data will show what counties had income above or below 100K.
Income <- Income %>%
mutate(Level = case_when(HHInc2022 < 100000 ~ "<$100K",
HHInc2022 > 99999 ~ "$100K+",
.default = "Error"))
head(Income, 10)
## GEOID District County HHInc2022 Significance HHInc2017
## 1 4702190022 District 1 Cheatham County 59741 Significant 44091
## 2 4702190212 District 2 Cheatham County 73634 Significant 55247
## 3 4702190402 District 3 Cheatham County 89861 Nonsignificant 73802
## 4 4702190592 District 4 Cheatham County 73293 Significant 49731
## 5 4702190782 District 5 Cheatham County 78380 Significant 60793
## 6 4702190972 District 6 Cheatham County 92305 Significant 66750
## 7 4703790038 District 1 Davidson County 75038 Significant 56258
## 8 4703790228 District 2 Davidson County 54346 Significant 32487
## 9 4703790418 District 3 Davidson County 67953 Significant 49402
## 10 4703790608 District 4 Davidson County 106260 Significant 79381
## Change Level
## 1 15650 <$100K
## 2 18387 <$100K
## 3 16059 <$100K
## 4 23562 <$100K
## 5 17587 <$100K
## 6 25555 <$100K
## 7 18780 <$100K
## 8 21859 <$100K
## 9 18551 <$100K
## 10 26879 $100K+
Since you are aggregating the data, the group_by function tells R what variables are to be put together. The summarize functions will count and total the information involved. The pivot function cleans up the data, while creating a new row for each of the groupings
LevelsByCounty <- Income %>%
group_by(County, Level) %>%
summarize(Count = n()) %>%
pivot_wider(names_from = Level,
values_from = Count)
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
head(LevelsByCounty, 10)
## # A tibble: 7 × 3
## # Groups: County [7]
## County `<$100K` `$100K+`
## <chr> <int> <int>
## 1 Cheatham County 6 NA
## 2 Davidson County 29 6
## 3 Robertson County 11 1
## 4 Rutherford County 18 3
## 5 Sumner County 10 2
## 6 Williamson County 2 10
## 7 Wilson County 15 10
I used the filter function, since we are looking for specific variables within one data frame to set up into another created data frame, which was named Rich Districts for all counties with districts including income above $100K
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+
I used the arrange function in the Rich District data frame since we want to order the data variables in descending order.
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+
The biggest trend, to no one’s surprise is that many of the higher income districts are in and around the Nashville area, including Williamson, Wilson, and Davidson Counties. Interesting enough, despite being around Nashville, Cheatham County has no districts with income above $100K. However, Davidson County, which has the most districts within the data set, it also has the most districts whose income is under $100K. Nashville has much to offer, however, economically, it not a rich district for its citizens