Population Data

The table below shows population data for Nashville-area counties from the U.S. Census Bureau’s American Community Survey. The table includes current and earlier population figures, population change, percent change, square miles, population density, and a density category. Davidson County has the highest population density in the table at about 1,419 people per square mile, while Hickman County has the lowest density at about 42 people per square mile. The table also shows that all of the counties experienced population growth between the two population measurements.

Population Data with Percent Change
County CurrentPop EarlierPop Region Change Percent_change Square_Miles Density Density_Category
Williamson 260351 208242 Doughnut 52109 0.25023 583 446.57118 Medium Density
Rutherford 360646 292425 Doughnut 68221 0.23329 620 581.68710 High Density
Wilson 158805 129918 Doughnut 28887 0.22235 571 278.11734 Medium Density
Maury 107791 88738 Non-doughnut 19053 0.21471 613 175.84176 Medium Density
Davidson 715388 598184 Davidson 117204 0.19593 504 1419.42063 High Density
Trousdale 11957 10131 Non-doughnut 1826 0.18024 114 104.88596 Medium Density
Sumner 204424 174773 Doughnut 29651 0.16965 529 386.43478 Medium Density
Macon 26240 23261 Non-doughnut 2979 0.12807 307 85.47231 Low Density
Robertson 75539 67517 Doughnut 8022 0.11881 476 158.69538 Medium Density
Dickson 55983 51608 Non-doughnut 4375 0.08477 490 114.25102 Medium Density
Cheatham 41829 39087 Doughnut 2742 0.07015 302 138.50662 Medium Density
Cannon 14818 13958 Non-doughnut 860 0.06161 266 55.70677 Low Density
Smith 20389 19389 Non-doughnut 1000 0.05158 314 64.93312 Low Density
Hickman 25436 24561 Non-doughnut 875 0.03563 612 41.56209 Low Density

Code:

# ============================================================
# Step 1: Install and load required 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 the County variable
# ============================================================

County <- c(
  "Cannon",
  "Cheatham",
  "Davidson",
  "Dickson",
  "Hickman",
  "Macon",
  "Maury",
  "Robertson",
  "Rutherford",
  "Smith",
  "Sumner",
  "Trousdale",
  "Williamson",
  "Wilson"
)


# ============================================================
# Step 3: Create the CurrentPop variable
# ============================================================

CurrentPop <- c(
  14818,
  41829,
  715388,
  55983,
  25436,
  26240,
  107791,
  75539,
  360646,
  20389,
  204424,
  11957,
  260351,
  158805
)


# ============================================================
# Step 4: Create the EarlierPop variable
# ============================================================

EarlierPop <- c(
  13958,
  39087,
  598184,
  51608,
  24561,
  23261,
  88738,
  67517,
  292425,
  19389,
  174773,
  10131,
  208242,
  129918
)


# ============================================================
# Step 5: Create the Region variable
# ============================================================

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: Create the Square_Miles variable
# ============================================================

Square_Miles <- c(
  266,
  302,
  504,
  490,
  612,
  307,
  613,
  476,
  620,
  314,
  529,
  114,
  583,
  571
)


# ============================================================
# Step 7: Create the Population data frame
# ============================================================

Population <- data.frame(
  County,
  CurrentPop,
  EarlierPop,
  Region,
  Square_Miles
)


# ============================================================
# Step 8: Sort Population by CurrentPop in descending order
# ============================================================

Population <- Population %>%
  arrange(desc(CurrentPop))


# ============================================================
# Step 9: Add a Change variable
# ============================================================

Population <- Population %>%
  mutate(Change = CurrentPop - EarlierPop)


# ============================================================
# Step 10: Create Change_only
# ============================================================

Change_only <- Population %>%
  select(County, Change) %>%
  arrange(desc(Change))


# ============================================================
# Step 11: Create Doughnut
# ============================================================

Doughnut <- Population %>%
  filter(Region == "Davidson" | Region == "Doughnut") %>%
  arrange(desc(Change))


# ============================================================
# Step 12: Create Summary
# ============================================================

Summary <- Population %>%
  group_by(Region) %>%
  summarize(
    CurrentPop = sum(CurrentPop),
    EarlierPop = sum(EarlierPop),
    Change = sum(Change)
  )


# ============================================================
# Step 13: Create Population_v2
# ============================================================

Population_v2 <- Population


# ============================================================
# Step 14: Add Percent_change
# ============================================================

Population_v2 <- Population_v2 %>%
  mutate(Percent_change = Change / EarlierPop)


# ============================================================
# Step 15: Sort Population_v2 by Percent_change
# ============================================================

Population_v2 <- Population_v2 %>%
  arrange(desc(Percent_change))


# ============================================================
# Step 16: Add Density
# ============================================================

Population_v2 <- Population_v2 %>%
  mutate(Density = CurrentPop / Square_Miles)


# ============================================================
# Step 17: Add Density_Category
# ============================================================

Population_v2 <- Population_v2 %>%
  mutate(
    Density_Category = case_when(
      Density >= 500 ~ "High Density",
      Density >= 100 ~ "Medium Density",
      Density < 100 ~ "Low Density"
    )
  )


# ============================================================
# Step 18: Create formatted kableExtra tables
# ============================================================

Population_table <- Population %>%
  kable(
    caption = "Population Data",
    format = "html"
  ) %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )


Change_only_table <- Change_only %>%
  kable(
    caption = "Population Change by County",
    format = "html"
  ) %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )


Doughnut_table <- Doughnut %>%
  kable(
    caption = "Davidson and Doughnut Counties",
    format = "html"
  ) %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )


Summary_table <- Summary %>%
  kable(
    caption = "Regional Population Totals",
    format = "html"
  ) %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )


# ============================================================
# Step 19: Create the updated Population_v2 table
# ============================================================

Population_v2_table <- Population_v2 %>%
  select(
    County,
    CurrentPop,
    EarlierPop,
    Region,
    Change,
    Percent_change,
    Square_Miles,
    Density,
    Density_Category
  ) %>%
  kable(
    caption = "Population Data with Percent Change",
    format = "html",
    digits = 5
  ) %>%
  kable_styling(
    bootstrap_options = c("striped", "hover", "condensed"),
    full_width = FALSE
  )


# ============================================================
# Step 20: Display the final Population_v2 table
# ============================================================

Population_v2_table

Data Source

U.S. Census Bureau, American Community Survey.

Data Source

U.S. Census Bureau, American Community Survey.