library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── 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
# Read data
dataReductions_df <- "cc-est2022-all.csv"
if (file.exists("cc-est2022-all.csv")) {
print("File exists.")
} else {
print("File not found.")
}
## [1] "File exists."
dataReductions_df <- read.csv("cc-est2022-all.csv")
# Data dictionary
Data_dictionary <- "https://www2.census.gov/programs-surveys/popest/technical-documentation/file-layouts/2020-2022/cc-est2022-alldata.pdf"
download.file(Data_dictionary, destfile = "cc-est2022-alldata.pdf", mode = "wb")
# Remove missing values from data frame
dataReductions_df <- dataReductions_df[complete.cases(dataReductions_df), ]
# Select needed attributes
selected_attributes <- dataReductions_df[, c("STNAME", "CTYNAME", "YEAR", "AGEGRP",
"TOT_POP", "TOT_MALE", "TOT_FEMALE")]
# Save new data set
save(selected_attributes, file = "Selected_Attributes.RData")
# Check if new file exists
if (file.exists("Selected_Attributes.RData")) {
load("Selected_Attributes.RData")
} else {
print("No file found.")
}
Data_set1 <- selected_attributes %>%
filter(AGEGRP == 0)
Data_set2 <- selected_attributes %>%
filter(AGEGRP != 0)
# Data split verification
if (nrow(Data_set1) + nrow(Data_set2) == nrow(selected_attributes)) {
print("Good!")
} else {
print("Bad!")
}
## [1] "Good!"
# Aggregate across Age groups
Aggregated_set <- Data_set2 %>%
group_by(STNAME, CTYNAME, YEAR) %>%
summarise(TOT_POP = sum(TOT_POP), TOT_MALE = sum(TOT_MALE),
TOT_FEMALE = sum(TOT_FEMALE))
## `summarise()` has grouped output by 'STNAME', 'CTYNAME'. You can override using
## the `.groups` argument.
Does this reduced data set have the same dimensions as set1?
Aggregated_set <- Aggregated_set %>%
group_by(STNAME, CTYNAME) %>%
summarise(TOT_POP = mean(TOT_POP), TOT_MALE = mean(TOT_MALE),
TOT_FEMALE = mean(TOT_FEMALE))
## `summarise()` has grouped output by 'STNAME'. You can override using the
## `.groups` argument.
State_populations <- Aggregated_set %>%
group_by(STNAME) %>%
summarise(Total_Population = sum(TOT_POP))
Total_pop <- sum(State_populations$Total_Population)
print(Total_pop)
## [1] 332070036
Is this true? (provide a reference) and does this make sense?