The table below shows Nashville-area counties sorted in descending
order by percent population change between the earlier and current
population estimates. These data, obtained from the U.S. Census Bureau’s
American Community Survey (ACS), provide a clear picture of how
population growth has varied across counties in the Nashville region.
Counties with the highest percent change experienced the fastest rates
of growth, suggesting increased residential development and migration
into those areas. In contrast, counties with lower percent change values
grew more slowly during the same period. More information can be found
on the ACS Website.
Code:
# ==================================================
# Step 1: Install and load required packages
# ==================================================
required_packages <- c("tidyverse", "knitr", "kableExtra")
for (pkg in required_packages) {
if (!requireNamespace(pkg, quietly = TRUE)) {
install.packages(pkg)
}
}
library(tidyverse)
library(knitr)
library(kableExtra)
# ==================================================
# Step 2: Create a vector of county names
# ==================================================
County <- c("Cannon", "Cheatham", "Davidson", "Dickson", "Hickman", "Macon",
"Maury", "Robertson", "Rutherford", "Smith", "Sumner", "Trousdale",
"Williamson", "Wilson")
# ==================================================
# Step 3: Create a vector of current population values
# ==================================================
CurrentPop <- c(14818, 41829, 715388, 55983, 25436, 26240,
107791, 75539, 360646, 20389, 204424,
11957, 260351, 158805)
# ==================================================
# Step 4: Create a vector of earlier population values
# ==================================================
EarlierPop <- c(13958, 39087, 598184, 51608, 24561, 23261,
88738, 67517, 292425, 19389, 174773,
10131, 208242, 129918)
# ==================================================
# Step 5: Create a vector of region classifications
# ==================================================
Region <- c("Non-doughnut", "Doughnut", "Davidson", "Non-doughnut",
"Non-doughnut", "Non-doughnut", "Non-doughnut",
"Doughnut", "Doughnut", "Non-doughnut",
"Doughnut", "Non-doughnut", "Doughnut", "Doughnut")
# ==================================================
# Step 6: Combine vectors into a Population data frame
# ==================================================
Population <- data.frame(
County,
CurrentPop,
EarlierPop,
Region
)
# ==================================================
# Step 7: Add a Change variable showing population
# growth
# ==================================================
Population <- Population %>%
mutate(Change = CurrentPop - EarlierPop)
# ==================================================
# Step 8: Display the updated Population data frame
# ==================================================
Population
# ==================================================
# Step 9: Create a Change_only data frame containing
# only the County and Change variables
# ==================================================
Change_only <- Population %>%
select(County, Change)
# ==================================================
# Step 10: Sort Change_only by Change in descending
# order
# ==================================================
Change_only <- Change_only %>%
arrange(desc(Change))
# ==================================================
# Step 11: Display the Change_only data frame
# ==================================================
Change_only
# ==================================================
# Step 12: Create a Doughnut data frame containing
# only Davidson and Doughnut regions
# ==================================================
Doughnut <- Population %>%
filter(Region %in% c("Davidson", "Doughnut"))
# ==================================================
# Step 13: Sort Doughnut by Change in descending
# order
# ==================================================
Doughnut <- Doughnut %>%
arrange(desc(Change))
# ==================================================
# Step 14: Display the Doughnut data frame
# ==================================================
Doughnut
# ==================================================
# Step 15: Create a Summary data frame containing
# population totals by Region
# ==================================================
Summary <- Population %>%
group_by(Region) %>%
summarize(
CurrentPop = sum(CurrentPop),
EarlierPop = sum(EarlierPop),
Change = sum(Change),
.groups = "drop"
)
# ==================================================
# Step 16: Display the Summary data frame
# ==================================================
Summary
# ==================================================
# Step 17: Copy Population into a new data frame
# called Population_v2
# ==================================================
Population_v2 <- Population
# ==================================================
# Step 18: Create a Percent_change variable
# ==================================================
Population_v2 <- Population_v2 %>%
mutate(Percent_change = Change / EarlierPop)
# ==================================================
# Step 19: Sort Population_v2 by Percent_change
# in descending order
# ==================================================
Population_v2 <- Population_v2 %>%
arrange(desc(Percent_change))
# ==================================================
# Step 20: Display the Population_v2 data frame
# ==================================================
Population_v2
# ==================================================
# Step 21: Create a kableExtra-formatted table for
# Population
# ==================================================
Population_tbl <- Population %>%
kable(
caption = "Population Data",
digits = 3
) %>%
kable_styling(full_width = FALSE)
# ==================================================
# Step 22: Create a kableExtra-formatted table for
# Change_only
# ==================================================
Change_only_tbl <- Change_only %>%
kable(
caption = "County Change Data",
digits = 3
) %>%
kable_styling(full_width = FALSE)
# ==================================================
# Step 23: Create a kableExtra-formatted table for
# Doughnut
# ==================================================
Doughnut_tbl <- Doughnut %>%
kable(
caption = "Davidson and Doughnut Counties",
digits = 3
) %>%
kable_styling(full_width = FALSE)
# ==================================================
# Step 24: Create a kableExtra-formatted table for
# Summary
# ==================================================
Summary_tbl <- Summary %>%
kable(
caption = "Regional Population Summary",
digits = 3
) %>%
kable_styling(full_width = FALSE)
# ==================================================
# Step 25: Create a kableExtra-formatted table for
# Population_v2
# ==================================================
Population_v2_tbl <- Population_v2 %>%
kable(
caption = "Population Data with Percent Change",
digits = 3
) %>%
kable_styling(full_width = FALSE)
# ==================================================
# Step 26: Display all formatted tables
# ==================================================
Population_tbl
Change_only_tbl
Doughnut_tbl
Summary_tbl
Population_v2_tbl