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.
The code with to install the packages is an add-on that allows easier to use alternatives when working with code in R.
Then the other set of code is used to import all the data points and download all the put them under the files of Inc 2017 and Inc 2022
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")
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.
The ode left_join is used to merge Inc2022 and Inc2017 data frames into a fdata frame called Income. The code: Inc2022 is to “to” data frame and the code Inc2017 if the from data from. The Inc2022 already had District, County and Significance columns and the GEOID in each data frame is the key column variable. m
Income <- left_join(Inc2022,Inc2017,
by = join_by(GEOID == GEOID))
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.
The mutate function was used to subtract your “To” data frame from the “From” data frame. This will then lead to the solution known as your “Change” variable. So it subtracted HHINCO2017 from HHInc2022 and the head() function showed the first 10 rows.
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
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.
I used the mutate and case_when function. The mutate function created the Level variable and the case function made the Level fuction equal to $100K+ if HHInc2022 was greater than 99999 and <$10000 if it was less than 100000.
Income <- Income %>%
mutate(Level = case_when(HHInc2022 > 99999 ~ "$100k+",
HHInc2022 < 100000 ~ "<$100k",
.default = "Error"))
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.
You are aggregating the data.
The Group_by function tells R what variables you are wanting to group together
The Summarize function conducts counts, totals, and other information/solutions with the grouped together variables
Then finally you use the pivot function helps make it look all nice and neat by cleaning it up creating a row for each of the grouped together function. In short teams “tidy”ing it up
LevelbyCounty <- 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(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
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.
The function used here is the Filter function filtered income for rows with a Level of $100k+ and it stored the results under a new data frame called RichDistricts
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+
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.
I used the arrange function and the descending fuction (desc()) to reorder the RichDisctricts data frame by the values in HHInc2022
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+
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
The trend I saw is pretty obvious in the case the highest growths of 100k+ were in Williamson and Wilson. That also Cheatham county saw no 100k+ levels. Then the biggest area with the <100k level being Davidson county which is where Nashville is located. This shows that Nashville is more than just country music and bars for the tourist. The income in the area is very low on the outskirts of the states capital.