if(!require(tidyverse))install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyverse)
mydata <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/AgeData.csv")
head(mydata, 10)
## GEOID County Under18_2017 Under18_2022 Significance
## 1 47001 Anderson County, Tennessee 15963 16413 Significant
## 2 47003 Bedford County, Tennessee 12068 12669 Significant
## 3 47005 Benton County, Tennessee 3209 3112 Nonsignificant
## 4 47007 Bledsoe County, Tennessee 2380 2428 Nonsignificant
## 5 47009 Blount County, Tennessee 26568 26988 Significant
## 6 47011 Bradley County, Tennessee 23210 23576 Significant
## 7 47013 Campbell County, Tennessee 8130 8091 Nonsignificant
## 8 47015 Cannon County, Tennessee 2957 3099 Significant
## 9 47017 Carroll County, Tennessee 6004 6173 Significant
## 10 47019 Carter County, Tennessee 10840 10157 Significant
mydata <- mydata %>%
mutate(Change = Under18_2022 - Under18_2017)
mydata <- mydata %>%
mutate(Direction = case_when(Change < 0 ~ "Loss",
Change == 0 ~ "No change",
Change > 0 ~ "Gain",
.default = "Error"))
head(mydata, 10)
## GEOID County Under18_2017 Under18_2022 Significance
## 1 47001 Anderson County, Tennessee 15963 16413 Significant
## 2 47003 Bedford County, Tennessee 12068 12669 Significant
## 3 47005 Benton County, Tennessee 3209 3112 Nonsignificant
## 4 47007 Bledsoe County, Tennessee 2380 2428 Nonsignificant
## 5 47009 Blount County, Tennessee 26568 26988 Significant
## 6 47011 Bradley County, Tennessee 23210 23576 Significant
## 7 47013 Campbell County, Tennessee 8130 8091 Nonsignificant
## 8 47015 Cannon County, Tennessee 2957 3099 Significant
## 9 47017 Carroll County, Tennessee 6004 6173 Significant
## 10 47019 Carter County, Tennessee 10840 10157 Significant
## Change Direction
## 1 450 Gain
## 2 601 Gain
## 3 -97 Loss
## 4 48 Gain
## 5 420 Gain
## 6 366 Gain
## 7 -39 Loss
## 8 142 Gain
## 9 169 Gain
## 10 -683 Loss