getwd()
## [1] "/Users/idlhy/Library/CloudStorage/OneDrive-개인/R FILE"
setwd("/Volumes/NO NAME/1. Data/ACS/Demographic")
library(censusapi)
##
## Attaching package: 'censusapi'
## The following object is masked from 'package:methods':
##
## getFunction
library(tidycensus)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── 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(magrittr)
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:purrr':
##
## set_names
##
## The following object is masked from 'package:tidyr':
##
## extract
library(readxl)
census_api_key("a8f3c16456333d1a2f2df056b992cffeaab717b8"
, install = TRUE
, overwrite=TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "a8f3c16456333d1a2f2df056b992cffeaab717b8"
Sys.getenv("CENSUS_API_KEY") #check my api key number
## [1] "a8f3c16456333d1a2f2df056b992cffeaab717b8"
years <- 2005:2019
years <- c(years, 2021)
names(years) <- years
pop <- map_dfr(years, ~{
get_acs(
geography = "state",
variables = c(m_w_pop_10to14 = "B01001H_005",
m_w_pop_15to17 = "B01001H_006",
m_w_pop_18to19 = "B01001H_007",
m_w_pop_20to24 = "B01001H_008",
m_w_pop_25to29 = "B01001H_009",
m_w_pop = "B01001H_002",
f_w_pop_10to14 = "B01001H_020",
f_w_pop_15to17 = "B01001H_021",
f_w_pop_18to19 = "B01001H_022",
f_w_pop_20to24 = "B01001H_023",
f_w_pop_25to29 = "B01001H_024",
f_w_pop = "B01001H_017",
m_b_pop_10to14 = "B01001B_005",
m_b_pop_15to17 = "B01001B_006",
m_b_pop_18to19 = "B01001B_007",
m_b_pop_20to24 = "B01001B_008",
m_b_pop_25to29 = "B01001B_009",
m_b_pop = "B01001B_002",
f_b_pop_10to14 = "B01001B_020",
f_b_pop_15to17 = "B01001B_021",
f_b_pop_18to19 = "B01001B_022",
f_b_pop_20to24 = "B01001B_023",
f_b_pop_25to29 = "B01001B_024",
f_b_pop = "B01001B_017",
m_h_pop_10to14 = "B01001I_005",
m_h_pop_15to17 = "B01001I_006",
m_h_pop_18to19 = "B01001I_007",
m_h_pop_20to24 = "B01001I_008",
m_h_pop_25to29 = "B01001I_009",
m_h_pop = "B01001I_002",
f_h_pop_10to14 = "B01001I_020",
f_h_pop_15to17 = "B01001I_021",
f_h_pop_18to19 = "B01001I_022",
f_h_pop_20to24 = "B01001I_023",
f_h_pop_25to29 = "B01001I_024",
f_h_pop = "B01001I_017"
),
survey = "acs1",
year = .x)
}, .id = "year")
## Getting data from the 2005 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2006 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2007 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2008 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2009 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2010 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2011 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2012 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2013 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2014 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2015 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2016 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2017 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2018 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2021 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
head(pop)
## # A tibble: 6 × 6
## year GEOID NAME variable estimate moe
## <chr> <chr> <chr> <chr> <dbl> <dbl>
## 1 2005 01 Alabama m_b_pop 519249 4008
## 2 2005 01 Alabama m_b_pop_10to14 50227 3612
## 3 2005 01 Alabama m_b_pop_15to17 29024 1235
## 4 2005 01 Alabama m_b_pop_18to19 17742 1532
## 5 2005 01 Alabama m_b_pop_20to24 43357 1859
## 6 2005 01 Alabama m_b_pop_25to29 36250 1988
pop_wide <- pop %>%
select(-moe) %>%
pivot_wider(names_from = c(variable, year),
names_sep = "_",
values_from = "estimate")
head(pop_wide)
## # A tibble: 6 × 578
## GEOID NAME m_b_pop_2005 m_b_pop_10to14_2005 m_b_pop_15to17_2005
## <chr> <chr> <dbl> <dbl> <dbl>
## 1 01 Alabama 519249 50227 29024
## 2 02 Alaska 10845 1442 564
## 3 04 Arizona 93204 9533 5242
## 4 05 Arkansas 188432 20199 12207
## 5 06 California 1028026 111747 61309
## 6 08 Colorado 82968 6692 3819
## # ℹ 573 more variables: m_b_pop_18to19_2005 <dbl>, m_b_pop_20to24_2005 <dbl>,
## # m_b_pop_25to29_2005 <dbl>, f_b_pop_2005 <dbl>, f_b_pop_10to14_2005 <dbl>,
## # f_b_pop_15to17_2005 <dbl>, f_b_pop_18to19_2005 <dbl>,
## # f_b_pop_20to24_2005 <dbl>, f_b_pop_25to29_2005 <dbl>, m_w_pop_2005 <dbl>,
## # m_w_pop_10to14_2005 <dbl>, m_w_pop_15to17_2005 <dbl>,
## # m_w_pop_18to19_2005 <dbl>, m_w_pop_20to24_2005 <dbl>,
## # m_w_pop_25to29_2005 <dbl>, f_w_pop_2005 <dbl>, f_w_pop_10to14_2005 <dbl>, …
pop_long <- pop_wide %>%
pivot_longer(cols = m_b_pop_2005:f_h_pop_25to29_2021,
names_to = c(".value", "year"),
names_pattern = "(.*)_(\\d{4})",
values_to = c("pop"))
head(pop_long)
## # A tibble: 6 × 39
## GEOID NAME year m_b_pop m_b_pop_10to14 m_b_pop_15to17 m_b_pop_18to19
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 01 Alabama 2005 519249 50227 29024 17742
## 2 01 Alabama 2006 566563 54413 31905 24339
## 3 01 Alabama 2007 564365 48942 32140 26206
## 4 01 Alabama 2008 566475 48275 31713 25858
## 5 01 Alabama 2009 572904 49562 30676 22166
## 6 01 Alabama 2010 587644 50671 33770 25637
## # ℹ 32 more variables: m_b_pop_20to24 <dbl>, m_b_pop_25to29 <dbl>,
## # f_b_pop <dbl>, f_b_pop_10to14 <dbl>, f_b_pop_15to17 <dbl>,
## # f_b_pop_18to19 <dbl>, f_b_pop_20to24 <dbl>, f_b_pop_25to29 <dbl>,
## # m_w_pop <dbl>, m_w_pop_10to14 <dbl>, m_w_pop_15to17 <dbl>,
## # m_w_pop_18to19 <dbl>, m_w_pop_20to24 <dbl>, m_w_pop_25to29 <dbl>,
## # f_w_pop <dbl>, f_w_pop_10to14 <dbl>, f_w_pop_15to17 <dbl>,
## # f_w_pop_18to19 <dbl>, f_w_pop_20to24 <dbl>, f_w_pop_25to29 <dbl>, …