Task 1: Using the code I have provided above, 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.
Response: The code imbedded (tidyverse) allows you to download other pieces of data. The other two pieces give you the raw data you need to use but not organized. 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: 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.
Response: The Left_ join is used to merge the Inc2022 and Inc2017 data is used to merge into a data frame called income. The Inc2022 is the “to” and Inc2017 is “from” which both puts the data into a district, county, and significance column to seperate the data.
Income <- left_join(Inc2022,Inc2017, by = join_by(GEOID == GEOID))
Task 3: 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.
Response: The mutate function subtracts the 2022 to 2017 data which then gives you the change variable, which then gives you the first 10 rows of data.
Income <- Income %>% mutate(Change = HHInc2022 - HHInc2017) head(Income, 10)
Task 4: 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.
Response: I used the mutate and case functions to get the “Level” variable. Mutate created Level as a whole, and the case function made it equal to $100k+ if HHInc2022 is greater than 99999 and less than $100,000 if it was less than 100000
Income <- Income %>% mutate(Level = case_when(HHInc2022 > 99999 ~ “$100k+”, HHInc2022 < 100000 ~ “<$100k”, .default = “Error”))
Task 5: 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.
Response: In this step we’re aggregating data. The group by step tells R what variables you want together. The summarize function shows the counts, totals, and other information with the grouped variables. Finally, you use the pivot function basically cleans everything up and makes it tidy.
LevelbyCounty <- Income %>% group_by(County, Level) %>% summarize(Count = n()) %>% pivot_wider(names_from = Level, values_from = Count)
Task 6: 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.
Response: The function here is the filter function where it filters rows based off income and anything $100k+ is stored in a new data frame called RichDistricts.
RichDistricts <- Income %>% filter(Level == “$100k+”) head(RichDistricts, 10)
Task 7: 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.
Response: In this step I used the arrange and descending function to reorder the rich districts based upon the values in HHInc2022.
RichDistricts <- RichDistricts %>% arrange(desc(HHInc2022)) head(RichDistricts, 10)
Task 8: 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.
Response: The trend I see is that districts within Williamson and Wilson county dominated the $100k+ territory. Another thing I found a little shocking was the lack of $100k+ income from Davidson county (Nashville area). That area features the big city obviously but also some of the richer areas in the state like a Brentwood. It just shows that the outskirts of the county have low income spots which shows Davidson Co. isn’t the ultra wealthy area as a whole as some people think.