Intro

Williamson and Rutherford counties grew faster than Davidson County over the past five years, even though Davidson picked up more people, the latest Census data show. In terms of growth per 100 residents among counties in the Nashville Metropolitan Statistical Area, Davidson County ranked fifth, behind Williamson, Rutherford, Wilson, and Maury counties. Davidson did lead the region in total growth, however, logging 117,204 new residents during the period. Rutherford came in second by that measure, with 68,221 new residents, and Williamson placed third with 52,109.

This table shows details for each county, with the data sorted by the changed per 100 residents. Williamson, for example, grew by 25 percent, or 25 new residents for every 100 original residents. Davidson, by contrast, grew about 20 percent.

##        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

# ==============================================================
# Step 1: Install (if needed) and load the tidyverse, knitr, and
#         kableExtra packages
# ==============================================================
if (!requireNamespace("tidyverse", quietly = TRUE)) {
  install.packages("tidyverse")
}
if (!requireNamespace("knitr", quietly = TRUE)) {
  install.packages("knitr")
}
if (!requireNamespace("kableExtra", quietly = TRUE)) {
  install.packages("kableExtra")
}
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 figures
#         (one value per county, in the same order as County)
# ==============================================================
CurrentPop <- c(14818, 41829, 715388, 55983, 25436, 26240, 107791, 75539, 360646,
                20389, 204424, 11957, 260351, 158805)

# ==============================================================
# Step 4: Create a vector of earlier population figures
#         (one value per county, in the same order as County)
# ==============================================================
EarlierPop <- c(13958, 39087, 598184, 51608, 24561, 23261, 88738, 67517, 292425,
                19389, 174773, 10131, 208242, 129918)

# ==============================================================
# Step 5: Create a vector classifying each county's region
#         (Doughnut, Non-doughnut, or Davidson)
# ==============================================================
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 the vectors into a single data frame
# ==============================================================
Population <- data.frame(
  County,
  CurrentPop,
  EarlierPop,
  Region
)

# ==============================================================
# Step 7: Display the resulting data frame
# ==============================================================
Population

# ==============================================================
# Step 8: Sort the Population data frame by CurrentPop,
#         in descending order
# ==============================================================
Population <- Population %>%
  arrange(desc(CurrentPop))

# ==============================================================
# Step 9: Redisplay the Population data frame (now sorted)
# ==============================================================
Population

# ==============================================================
# Step 10: Use mutate() to add a "Change" variable, calculated
#          as CurrentPop minus EarlierPop
# ==============================================================
Population <- Population %>%
  mutate(Change = CurrentPop - EarlierPop)

# ==============================================================
# Step 11: Display the updated data frame
# ==============================================================
Population

# ==============================================================
# Step 12: Use select() to create a new data frame, Change_only,
#          containing just County and Change, then use arrange()
#          to sort it by Change in descending order
# ==============================================================
Change_only <- Population %>%
  select(County, Change) %>%
  arrange(desc(Change))

# ==============================================================
# Step 13: Display the Change_only data frame
# ==============================================================
Change_only

# ==============================================================
# Step 14: Use filter() to create a new data frame, Doughnut,
#          keeping only rows where Region is "Davidson" or
#          "Doughnut", then use arrange() to sort it by Change
#          in descending order
# ==============================================================
Doughnut <- Population %>%
  filter(Region %in% c("Davidson", "Doughnut")) %>%
  arrange(desc(Change))

# ==============================================================
# Step 15: Display the Doughnut data frame
# ==============================================================
Doughnut

# ==============================================================
# Step 16: Use group_by() and summarize() to create a Summary
#          data frame that totals CurrentPop, EarlierPop, and
#          Change from the Population data frame, grouped by
#          Region
# ==============================================================
Summary <- Population %>%
  group_by(Region) %>%
  summarize(
    CurrentPop = sum(CurrentPop),
    EarlierPop = sum(EarlierPop),
    Change = sum(Change)
  )

# ==============================================================
# Step 17: Display the Summary data frame
# ==============================================================
Summary

# ==============================================================
# Step 18: Copy Population into a new data frame, Population_v2,
#          use mutate() to add a Percent_change variable
#          (Change divided by EarlierPop), and sort the result
#          by Percent_change in descending order
# ==============================================================
Population_v2 <- Population %>%
  mutate(Percent_change = Change / EarlierPop) %>%
  arrange(desc(Percent_change))

# ==============================================================
# Step 19: Display the Population_v2 data frame
# ==============================================================
Population_v2

# ==============================================================
# Step 20: Create a kableExtra-formatted table for the
#          Population data frame, then display it
# ==============================================================
Population_table <- Population %>%
  kbl(caption = "Population") %>%
  kable_styling()
Population_table

# ==============================================================
# Step 21: Create a kableExtra-formatted table for the
#          Change_only data frame, then display it
# ==============================================================
Change_only_table <- Change_only %>%
  kbl(caption = "Change_only") %>%
  kable_styling()
Change_only_table

# ==============================================================
# Step 22: Create a kableExtra-formatted table for the
#          Doughnut data frame, then display it
# ==============================================================
Doughnut_table <- Doughnut %>%
  kbl(caption = "Doughnut") %>%
  kable_styling()
Doughnut_table

# ==============================================================
# Step 23: Create a kableExtra-formatted table for the
#          Summary data frame, then display it
# ==============================================================
Summary_table <- Summary %>%
  kbl(caption = "Summary") %>%
  kable_styling()
Summary_table

# ==============================================================
# Step 24: Create a kableExtra-formatted table for the
#          Population_v2 data frame, then display it
# ==============================================================
Population_v2_table <- Population_v2 %>%
  kbl(caption = "Population_v2") %>%
  kable_styling()
Population_v2_table