Looking through the past five years, the biggest total change in population came from Davidson County while Williamson had the largest percent change of any county according to updated census data. The smallest change came in Hickman County who gained a total of 875 people which equated to roughly three percent or 3 residents for every 100. Davidson still has the largest total population with Rutherford, Williamson, Sumner, and Wilson rounding out the top five.
This table outlines these changes with the total population change and percentage population change displayed. The percentage change is populated on the ratio of every 100 people in the county.
## County CurrentPop EarlierPop Region Change Percent_change
## 1 Williamson 260351 208242 Doughnut 52109 0.25023290
## 2 Rutherford 360646 292425 Doughnut 68221 0.23329401
## 3 Wilson 158805 129918 Doughnut 28887 0.22234794
## 4 Maury 107791 88738 Non-doughnut 19053 0.21471072
## 5 Davidson 715388 598184 Davidson 117204 0.19593302
## 6 Trousdale 11957 10131 Non-doughnut 1826 0.18023887
## 7 Sumner 204424 174773 Doughnut 29651 0.16965435
## 8 Macon 26240 23261 Non-doughnut 2979 0.12806844
## 9 Robertson 75539 67517 Doughnut 8022 0.11881452
## 10 Dickson 55983 51608 Non-doughnut 4375 0.08477368
## 11 Cheatham 41829 39087 Doughnut 2742 0.07015120
## 12 Cannon 14818 13958 Non-doughnut 860 0.06161341
## 13 Smith 20389 19389 Non-doughnut 1000 0.05157564
## 14 Hickman 25436 24561 Non-doughnut 875 0.03562559
Code
The code below generated the output for the table. It is taken from the U.S. Census Bureau’s American Community Survey.
# 1. Install tidyverse, knitr, and kableExtra if not already installed; then load them
if (!require(tidyverse)) {
install.packages("tidyverse")
}
if (!require(knitr)) {
install.packages("knitr")
}
if (!require(kableExtra)) {
install.packages("kableExtra")
}
library(tidyverse)
library(knitr)
library(kableExtra)
# 2. Define county names
County <- c("Cannon", "Cheatham", "Davidson", "Dickson", "Hickman", "Macon",
"Maury", "Robertson", "Rutherford", "Smith", "Sumner", "Trousdale",
"Williamson", "Wilson")
# 3. Define current population values
CurrentPop <- c(14818, 41829, 715388, 55983, 25436, 26240, 107791, 75539, 360646,
20389, 204424, 11957, 260351, 158805)
# 4. Define earlier population values
EarlierPop <- c(13958, 39087, 598184, 51608, 24561, 23261, 88738, 67517, 292425,
19389, 174773, 10131, 208242, 129918)
# 5. Define region classification for each county
Region <- c("Non-doughnut", "Doughnut", "Davidson", "Non-doughnut", "Non-doughnut",
"Non-doughnut", "Non-doughnut", "Doughnut", "Doughnut", "Non-doughnut",
"Doughnut", "Non-doughnut", "Doughnut", "Doughnut")
# 6. Combine all vectors into a single data frame
Population <- data.frame(
County,
CurrentPop,
EarlierPop,
Region
)
# 7. Sort Population by CurrentPop in descending order
Population <- Population %>% arrange(desc(CurrentPop))
# 8. Add Change variable showing population growth
Population <- Population %>% mutate(Change = CurrentPop - EarlierPop)
# 9. Display the updated Population data frame
Population
# 10. Create Change_only data frame with only County and Change
Change_only <- Population %>% select(County, Change)
# 11. Sort Change_only by Change in descending order
Change_only <- Change_only %>% arrange(desc(Change))
# 12. Display the Change_only data frame
Change_only
# 13. Create Doughnut data frame containing only Davidson or Doughnut rows
Doughnut <- Population %>% filter(Region %in% c("Davidson", "Doughnut"))
# 14. Sort Doughnut data frame by Change in descending order
Doughnut <- Doughnut %>% arrange(desc(Change))
# 15. Display the Doughnut data frame
Doughnut
# 16. Create Summary data frame that totals CurrentPop, EarlierPop, and Change by Region
Summary <- Population %>%
group_by(Region) %>%
summarize(
Total_CurrentPop = sum(CurrentPop),
Total_EarlierPop = sum(EarlierPop),
Total_Change = sum(Change)
)
# 17. Display the Summary data frame
Summary
# 18. Copy Population data into Population_v2
Population_v2 <- Population
# 19. Add Percent_change variable using mutate()
Population_v2 <- Population_v2 %>% mutate(Percent_change = Change / EarlierPop)
# 20. Sort Population_v2 by Percent_change in descending order
Population_v2 <- Population_v2 %>% arrange(desc(Percent_change))
# 21. Display the Population_v2 data frame
Population_v2
# 22. Create kableExtra-formatted tables for all data frames
Population_kable <- Population %>%
kable("html", caption = "Population Data Frame") %>%
kable_styling(full_width = FALSE)
Change_only_kable <- Change_only %>%
kable("html", caption = "Change Only Data Frame") %>%
kable_styling(full_width = FALSE)
Doughnut_kable <- Doughnut %>%
kable("html", caption = "Doughnut Region Data Frame") %>%
kable_styling(full_width = FALSE)
Summary_kable <- Summary %>%
kable("html", caption = "Summary by Region") %>%
kable_styling(full_width = FALSE)
Population_v2_kable <- Population_v2 %>%
kable("html", caption = "Population_v2 with Percent Change") %>%
kable_styling(full_width = FALSE)
# 23. Display all kableExtra tables
Population_kable
Change_only_kable
Doughnut_kable
Summary_kable
Population_v2_kable