Task 1
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")
The tidyverse package allows for more coding abilities. The data sets are pulled from the links using the “read” code.
Task 2
Income <- left_join(Inc2017,
Inc2022,
by = join_by(GEOID == GEOID))
head(Income, 10)
## GEOID HHInc2017 District County HHInc2022 Significance
## 1 4702190022 44091 District 1 Cheatham County 59741 Significant
## 2 4702190212 55247 District 2 Cheatham County 73634 Significant
## 3 4702190402 73802 District 3 Cheatham County 89861 Nonsignificant
## 4 4702190592 49731 District 4 Cheatham County 73293 Significant
## 5 4702190782 60793 District 5 Cheatham County 78380 Significant
## 6 4702190972 66750 District 6 Cheatham County 92305 Significant
## 7 4703790038 56258 District 1 Davidson County 75038 Significant
## 8 4703790228 32487 District 2 Davidson County 54346 Significant
## 9 4703790418 49402 District 3 Davidson County 67953 Significant
## 10 4703790608 79381 District 4 Davidson County 106260 Significant
Joining Inc2017 and Inc2022 was done using the GEOID, as they both had the same ones, just different variables.
Task 3
Income <- Income %>%
mutate(Change = HHInc2022 - HHInc2017)
head(Income, 10)
## GEOID HHInc2017 District County HHInc2022 Significance
## 1 4702190022 44091 District 1 Cheatham County 59741 Significant
## 2 4702190212 55247 District 2 Cheatham County 73634 Significant
## 3 4702190402 73802 District 3 Cheatham County 89861 Nonsignificant
## 4 4702190592 49731 District 4 Cheatham County 73293 Significant
## 5 4702190782 60793 District 5 Cheatham County 78380 Significant
## 6 4702190972 66750 District 6 Cheatham County 92305 Significant
## 7 4703790038 56258 District 1 Davidson County 75038 Significant
## 8 4703790228 32487 District 2 Davidson County 54346 Significant
## 9 4703790418 49402 District 3 Davidson County 67953 Significant
## 10 4703790608 79381 District 4 Davidson County 106260 Significant
## Change
## 1 15650
## 2 18387
## 3 16059
## 4 23562
## 5 17587
## 6 25555
## 7 18780
## 8 21859
## 9 18551
## 10 26879
Since the 2022 data is more recent, to find the change you subtract the 2017 data from that. The mutate function adds a new column for the change.
Task 4
Income <- Income %>%
mutate(Level = case_when(HHInc2022 < 100000 ~ "<$100k",
HHInc2022 == 0 ~ "$100k",
HHInc2022 > 100000 ~ "$100k+",
.default = "Error"))
head(Income, 10)
## GEOID HHInc2017 District County HHInc2022 Significance
## 1 4702190022 44091 District 1 Cheatham County 59741 Significant
## 2 4702190212 55247 District 2 Cheatham County 73634 Significant
## 3 4702190402 73802 District 3 Cheatham County 89861 Nonsignificant
## 4 4702190592 49731 District 4 Cheatham County 73293 Significant
## 5 4702190782 60793 District 5 Cheatham County 78380 Significant
## 6 4702190972 66750 District 6 Cheatham County 92305 Significant
## 7 4703790038 56258 District 1 Davidson County 75038 Significant
## 8 4703790228 32487 District 2 Davidson County 54346 Significant
## 9 4703790418 49402 District 3 Davidson County 67953 Significant
## 10 4703790608 79381 District 4 Davidson County 106260 Significant
## 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+
The function “mutate” creates a new column to house the variable created. “Case_when” allows R to fill in the blank based on what equation is correct.
Task 5
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
The “group_by” function creates a group that isn’t shown but is embedded within the code. The “summarize” code brings it to light as it groups by the count. Finally, the “pivot_Wider” code adds more columns and removes a few rows to make everything cleaner.
Task 6
RichDistricts <- filter(Income, Level == "$100k+")
head(RichDistricts, 10)
## GEOID HHInc2017 District County HHInc2022 Significance
## 1 4703790608 79381 District 4 Davidson County 106260 Significant
## 2 4703794218 86768 District 23 Davidson County 117474 Significant
## 3 4703794408 79959 District 24 Davidson County 110739 Significant
## 4 4703794598 90998 District 25 Davidson County 120206 Significant
## 5 4703796460 128421 District 34 Davidson County 161370 Significant
## 6 4703796650 99666 District 35 Davidson County 117294 Significant
## 7 4714791098 65179 District 6 Robertson County 106058 Significant
## 8 4714991480 75875 District 8 Rutherford County 102862 Significant
## 9 4714991670 77481 District 9 Rutherford County 103006 Significant
## 10 4714992620 95708 District 14 Rutherford County 105750 Nonsignificant
## 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+
The “filter” function removes every piece of data that doesn’t fit the set rule.
Task 7
RichDistricts <- arrange(RichDistricts,desc(HHInc2022))
head(RichDistricts, 10)
## GEOID HHInc2017 District County HHInc2022 Significance
## 1 4718791328 150106 District 7 Williamson County 181709 Significant
## 2 4718791138 154149 District 6 Williamson County 178665 Significant
## 3 4703796460 128421 District 34 Davidson County 161370 Significant
## 4 4718790948 121622 District 5 Williamson County 159737 Significant
## 5 4718791708 117526 District 9 Williamson County 144924 Significant
## 6 4718993040 74528 District 16 Wilson County 125785 Significant
## 7 4718990570 80577 District 3 Wilson County 125324 Significant
## 8 4718790758 127552 District 4 Williamson County 124237 Nonsignificant
## 9 4718790378 101687 District 2 Williamson County 123609 Significant
## 10 4718990380 98846 District 2 Wilson County 120302 Significant
## 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 “arrange” function reorganizes the data set in whatever order is stated.
Task 8 I was shocked to see that where MTSU lies is one of the least rich districts. It’s also shocking to me how few counties have $100k+ in income. All in all this excercize was very eye opening.