Task 1: This code, if needed, will install the tidyverse package and load it. Next, it uses the read.csv function to download the Inc2022.csv and load it, and name the data frame Inc2022. The last line in this block uses the read.csv as well to download the Inc2017.csv and load it, and name the data frame Inc2017.
if(!require(tidyverse))install.packages("tidyverse")
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: This code uses the left_join function to join the Inc2022 and Inc2017 data frames together and into a new data frame and name it “Income”. Inc2022 was the data frame added to and Inc2017 was the data frame the function took data from and they were joined by the key column named GEIOD. The head function was used to show the first 10 columns of the new merged data.
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
Task 3: This code adds the variable “Change” to the Income data frame using the mutate function. The head function shows the first 10 rows of the Income data frame with the new “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
Task 4: This code adds the variable “Level” to the Income data frame using the mutate function in tandem with the case_when function. The head function shows the first 10 rows of the Income data frame with the new “Level” variable.
Income <- Income %>%
mutate(Level = case_when(HHInc2022 < 100000 ~ "<$100k",
HHInc2022 == 0 ~ "No change",
HHInc2022 > 100000 ~ "$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+
Task 5: This code creates the new data frame named “LevelByCounty”. The code also uses the group_by function to group the new data frame by the Level variable. Next, using the summarize function, it takes the count of the number of the Level variable and places them in the count variable. Afterward, using the pivot_wider function it separates the two values in the Level into their column. The head function shows the new LevelbyCounty data frame’s first ten rows.
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)
head(LevelByCounty, 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
Task 6 & 7: This code creates a new data frame named “RichDistricts” and then uses the filter function to filter the Income data frame by the “Level” variable that falls under the 100k+ column. Next, the arrange function arranges the RichDistricts data frame in descending order by the HHInc2022 variable. The head function shows the first 10 rows of the RichDistricts data frame.
RichDistricts <- filter(Income, Level == "$100k+")
RichDistricts <- arrange(RichDistricts, 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+
Task 8: One thing I noticed was the consistency of Williamson County. Even the nonsignificant changes of some of their districts still cracked near the top of the change and levels variable while the rest of the county seemed to fluctuate throughout the whole process.