==================================================
Step 1: Install and load required packages
==================================================
if (!require(tidyverse)) { install.packages(“tidyverse”)
library(tidyverse) } else { library(tidyverse) }
if (!require(knitr)) { install.packages(“knitr”) library(knitr) }
else { library(knitr) }
if (!require(kableExtra)) { install.packages(“kableExtra”)
library(kableExtra) } else { library(kableExtra) }
==================================================
Step 2: Create vectors containing county names
==================================================
County <- c( “Cannon”, “Cheatham”, “Davidson”, “Dickson”,
“Hickman”, “Macon”, “Maury”, “Robertson”, “Rutherford”, “Smith”,
“Sumner”, “Trousdale”, “Williamson”, “Wilson” )
==================================================
Step 3: Create vector containing current population
data for each county
==================================================
CurrentPop <- c( 14818, 41829, 715388, 55983, 25436, 26240,
107791, 75539, 360646, 20389, 204424, 11957, 260351, 158805 )
==================================================
Step 4: Create vector containing earlier population
data for each county
==================================================
EarlierPop <- c( 13958, 39087, 598184, 51608, 24561, 23261, 88738,
67517, 292425, 19389, 174773, 10131, 208242, 129918 )
==================================================
Step 5: Create vector identifying the regional
classification of 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” )
==================================================
Step 6: Combine all vectors into a data frame
called Population
==================================================
Population <- data.frame( County, CurrentPop, EarlierPop, Region
)
==================================================
Step 7: Display the Population data frame
==================================================
Population
==================================================
Step 8: Sort the Population data frame by
CurrentPop in descending order
==================================================
Population <- Population %>% arrange(desc(CurrentPop))
==================================================
Step 9: Display the sorted Population data frame
==================================================
Population
==================================================
Step 10: Add a Change variable showing the
difference between CurrentPop and EarlierPop
==================================================
Population <- Population %>% mutate(Change = CurrentPop -
EarlierPop)
==================================================
Step 11: Display the updated data frame
with the Change variable
==================================================
Population
==================================================
Step 12: Create a data frame containing only
County and Change, sorted by Change descending
==================================================
Change_only <- Population %>% select(County, Change) %>%
arrange(desc(Change))
==================================================
Step 13: Display the Change_only data frame
==================================================
Change_only
==================================================
Step 14: Create a Doughnut data frame containing
only Davidson and Doughnut region counties,
sorted 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: Create a Summary data frame containing
total CurrentPop, EarlierPop, and Change values
grouped by Region
==================================================
Summary <- Population %>% group_by(Region) %>% summarize(
TotalCurrentPop = sum(CurrentPop), TotalEarlierPop = sum(EarlierPop),
TotalChange = sum(Change) )
==================================================
Step 17: Display the Summary data frame
==================================================
Summary
==================================================
Step 18: Create a copy of the Population data
frame named Population_v2
==================================================
Population_v2 <- Population
==================================================
Step 19: Add a Percent_change variable showing
proportional population change
==================================================
Population_v2 <- Population_v2 %>% mutate(Percent_change =
Change / EarlierPop)
==================================================
Step 20: Sort the Population_v2 data frame by
Percent_change in descending order
==================================================
Population_v2 <- Population_v2 %>%
arrange(desc(Percent_change))
==================================================
Step 21: Display the Population_v2 data frame
==================================================
Population_v2
==================================================
Step 22: Create a formatted table for Population
==================================================
Population_tbl <- Population %>% kable( caption = “Population
Data”, digits = 2 ) %>% kable_styling( bootstrap_options =
c(“striped”, “hover”, “condensed”), full_width = FALSE )
Population_tbl
==================================================
Step 23: Create a formatted table for Change_only
==================================================
Change_only_tbl <- Change_only %>% kable( caption = “County
Population Change”, digits = 2 ) %>% kable_styling( bootstrap_options
= c(