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 1 Response: This code installes the tidyverse package, if needed, and loads it. Next, it uses the read.csv() function to download the Inc2022.csv data and put it into a data frame called Inc2022. Finally, it uses the same function to download the Inc2017.csv data and put it into a data frame called Inc2017.
Income <- left_join(Inc2017,
Inc2022,
by= join_by(GEOID==GEOID))
Task 2 Response: Inc2017 was my “from” variable. Inc2022 is my “to” variable. GEOID were my “key column” variables.
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
Task 3 response: I used the mutate function to create the Change variable.
Income <- Income %>%
mutate(Level = case_when(HHInc2022 < 100000 ~ "<100k",
HHInc2022 == 100000 ~ "No change",
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+
Task 4 Response: I used the mutate and case_when function for the Level variable.
LevelbyCounty <- Income
head(LevelbyCounty,20)
## 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
## 11 4703790798 40220 District 5 Davidson County 71189 Significant
## 12 4703790988 50871 District 6 Davidson County 82996 Significant
## 13 4703791178 46594 District 7 Davidson County 72500 Significant
## 14 4703791368 39885 District 8 Davidson County 62232 Significant
## 15 4703791558 39769 District 9 Davidson County 54322 Significant
## 16 4703791748 47270 District 10 Davidson County 58338 Significant
## 17 4703791938 57086 District 11 Davidson County 73158 Significant
## 18 4703792128 61720 District 12 Davidson County 85559 Significant
## 19 4703792318 46435 District 13 Davidson County 63110 Significant
## 20 4703792508 53068 District 14 Davidson County 64380 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+
## 11 30969 <100k
## 12 32125 <100k
## 13 25906 <100k
## 14 22347 <100k
## 15 14553 <100k
## 16 11068 <100k
## 17 16072 <100k
## 18 23839 <100k
## 19 16675 <100k
## 20 11312 <100k
LevelbyCounty <- group_by(LevelbyCounty, County, Level)
head(LevelbyCounty,20)
## # A tibble: 20 × 8
## # Groups: County, Level [3]
## GEOID HHInc2017 District County HHInc2022 Significance Change Level
## <dbl> <int> <chr> <chr> <int> <chr> <int> <chr>
## 1 4702190022 44091 District 1 Cheatha… 59741 Significant 15650 <100k
## 2 4702190212 55247 District 2 Cheatha… 73634 Significant 18387 <100k
## 3 4702190402 73802 District 3 Cheatha… 89861 Nonsignific… 16059 <100k
## 4 4702190592 49731 District 4 Cheatha… 73293 Significant 23562 <100k
## 5 4702190782 60793 District 5 Cheatha… 78380 Significant 17587 <100k
## 6 4702190972 66750 District 6 Cheatha… 92305 Significant 25555 <100k
## 7 4703790038 56258 District 1 Davidso… 75038 Significant 18780 <100k
## 8 4703790228 32487 District 2 Davidso… 54346 Significant 21859 <100k
## 9 4703790418 49402 District 3 Davidso… 67953 Significant 18551 <100k
## 10 4703790608 79381 District 4 Davidso… 106260 Significant 26879 100k+
## 11 4703790798 40220 District 5 Davidso… 71189 Significant 30969 <100k
## 12 4703790988 50871 District 6 Davidso… 82996 Significant 32125 <100k
## 13 4703791178 46594 District 7 Davidso… 72500 Significant 25906 <100k
## 14 4703791368 39885 District 8 Davidso… 62232 Significant 22347 <100k
## 15 4703791558 39769 District 9 Davidso… 54322 Significant 14553 <100k
## 16 4703791748 47270 District 10 Davidso… 58338 Significant 11068 <100k
## 17 4703791938 57086 District 11 Davidso… 73158 Significant 16072 <100k
## 18 4703792128 61720 District 12 Davidso… 85559 Significant 23839 <100k
## 19 4703792318 46435 District 13 Davidso… 63110 Significant 16675 <100k
## 20 4703792508 53068 District 14 Davidso… 64380 Significant 11312 <100k
LevelbyCounty <- summarize(LevelbyCounty, Count = n())
## `summarise()` has grouped output by 'County'. You can override using the
## `.groups` argument.
head(LevelbyCounty,10)
## # A tibble: 10 × 3
## # Groups: County [6]
## County Level Count
## <chr> <chr> <int>
## 1 Cheatham County <100k 6
## 2 Davidson County 100k+ 6
## 3 Davidson County <100k 29
## 4 Robertson County 100k+ 1
## 5 Robertson County <100k 11
## 6 Rutherford County 100k+ 3
## 7 Rutherford County <100k 18
## 8 Sumner County 100k+ 2
## 9 Sumner County <100k 10
## 10 Williamson County 100k+ 10
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 5 Response: I used the group_by, summarize, and pivot_wider function.
RichDistricts <- Income
head(RichDistricts,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+
RichDistricts <- filter(RichDistricts, 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+
Task 6 Response: I used the filtering function.
RichDistricts <- RichDistricts %>%
arrange(desc(HHInc2022))
Task 7 Response: I used the arrange function.
Task 8 Response: One thing I thought was interesting is Williamson County is the only county with most districts making over 100k. Their districts have also all significantly increased in income since 2017.