# Set parameters
# Set your Census API key
census_api_key("YOUR_CENSUS_API_KEY")
# Define the year and survey type
survey <- "acs1"
# Define the years you want-pull data for
years <- c(2011, 2013, 2015, 2017, 2019, 2021, 2023)
# Define variables to pull from the Census API
variables <- c(
# S1201 marital status
"B12002_004E", # male, never married, 15-17 years
"B12002_005E", # male, never married, 18-19 years
"B12002_006E", # male, never married, 20-24 years
"B12002_007E", # male, never married, 25-29 years
"B12002_008E", # male, never married, 30-34 years
"B12002_009E", # male, never married, 35-39 years
"B12002_010E", # male, never married, 40-44 years
"B12002_011E", # male, never married, 45-49 years
"B12002_012E", # male, never married, 50-54 years
"B12002_013E", # male, never married, 55-59 years
"B12002_014E", # male, never married, 60-64 years
"B12002_015E", # male, never married, 65-74 years
"B12002_016E", # male, never married, 75-84 years
"B12002_017E", # male, never married, 85+ years
"B12002_020E", # male, married, 15-17 years
"B12002_021E", # male, married, 18-19 years
"B12002_022E", # male, married, 20-24 years
"B12002_023E", # male, married, 25-29 years
"B12002_024E", # male, married, 30-34 years
"B12002_025E", # male, married, 35-39 years
"B12002_026E", # male, married, 40-44 years
"B12002_027E", # male, married, 45-49 years
"B12002_028E", # male, married, 50-54 years
"B12002_029E", # male, married, 55-59 years
"B12002_030E", # male, married, 60-64 years
"B12002_031E", # male, married, 65-74 years
"B12002_032E", # male, married, 75-84 years
"B12002_033E", # male, married, 85+ years
"B12002_036E", # male, separated, 15-17 years
"B12002_037E", # male, separated, 18-19 years
"B12002_038E", # male, separated, 20-24 years
"B12002_039E", # male, separated, 25-29 years
"B12002_040E", # male, separated, 30-34 years
"B12002_041E", # male, separated, 35-39 years
"B12002_042E", # male, separated, 40-44 years
"B12002_043E", # male, separated, 45-49 years
"B12002_044E", # male, separated, 50-54 years
"B12002_045E", # male, separated, 55-59 years
"B12002_046E", # male, separated, 60-64 years
"B12002_047E", # male, separated, 65-74 years
"B12002_048E", # male, separated, 75-84 years
"B12002_049E", # male, separated, 85+ years
"B12002_066E", # male, widowed, 15-17 years
"B12002_067E", # male, widowed, 18-19 years
"B12002_068E", # male, widowed, 20-24 years
"B12002_069E", # male, widowed, 25-29 years
"B12002_070E", # male, widowed, 30-34 years
"B12002_071E", # male, widowed, 35-39 years
"B12002_072E", # male, widowed, 40-44 years
"B12002_073E", # male, widowed, 45-49 years
"B12002_074E", # male, widowed, 50-54 years
"B12002_075E", # male, widowed, 55-59 years
"B12002_076E", # male, widowed, 60-64 years
"B12002_077E", # male, widowed, 65-74 years
"B12002_078E", # male, widowed, 75-84 years
"B12002_079E", # male, widowed, 85+ years
"B12002_081E", # male, divorced, 15-17 years
"B12002_082E", # male, divorced, 18-19 years
"B12002_083E", # male, divorced, 20-24 years
"B12002_084E", # male, divorced, 25-29 years
"B12002_085E", # male, divorced, 30-34 years
"B12002_086E", # male, divorced, 35-39 years
"B12002_087E", # male, divorced, 40-44 years
"B12002_088E", # male, divorced, 45-49 years
"B12002_089E", # male, divorced, 50-54 years
"B12002_090E", # male, divorced, 55-59 years
"B12002_091E", # male, divorced, 60-64 years
"B12002_092E", # male, divorced, 65-74 years
"B12002_093E", # male, divorced, 75-84 years
"B12002_094E", # male, divorced, 85+ years
"B12002_097E", # female, never married, 15-17 years
"B12002_098E", # female, never married, 18-19 years
"B12002_099E", # female, never married, 20-24 years
"B12002_100E", # female, never married, 25-29 years
"B12002_101E", # female, never married, 30-34 years
"B12002_102E", # female, never married, 35-39 years
"B12002_103E", # female, never married, 40-44 years
"B12002_104E", # female, never married, 45-49 years
"B12002_105E", # female, never married, 50-54 years
"B12002_106E", # female, never married, 55-59 years
"B12002_107E", # female, never married, 60-64 years
"B12002_108E", # female, never married, 65-74 years
"B12002_109E", # female, never married, 75-84 years
"B12002_110E", # female, never married, 85+ years
"B12002_113E", # female, married, 15-17 years
"B12002_114E", # female, married, 18-19 years
"B12002_115E", # female, married, 20-24 years
"B12002_116E", # female, married, 25-29 years
"B12002_117E", # female, married, 30-34 years
"B12002_118E", # female, married, 35-39 years
"B12002_119E", # female, married, 40-44 years
"B12002_120E", # female, married, 45-49 years
"B12002_121E", # female, married, 50-54 years
"B12002_122E", # female, married, 55-59 years
"B12002_123E", # female, married, 60-64 years
"B12002_124E", # female, married, 65-74 years
"B12002_125E", # female, married, 75-84 years
"B12002_126E", # female, married, 85+ years
"B12002_129E", # female, separated, 15-17 years
"B12002_130E", # female, separated, 18-19 years
"B12002_131E", # female, separated, 20-24 years
"B12002_132E", # female, separated, 25-29 years
"B12002_133E", # female, separated, 30-34 years
"B12002_134E", # female, separated, 35-39 years
"B12002_135E", # female, separated, 40-44 years
"B12002_136E", # female, separated, 45-49 years
"B12002_137E", # female, separated, 50-54 years
"B12002_138E", # female, separated, 55-59 years
"B12002_139E", # female, separated, 60-64 years
"B12002_140E", # female, separated, 65-74 years
"B12002_141E", # female, separated, 75-84 years
"B12002_142E", # female, separated, 85+ years
"B12002_159E", # female, widowed, 15-17 years
"B12002_160E", # female, widowed, 18-19 years
"B12002_161E", # female, widowed, 20-24 years
"B12002_162E", # female, widowed, 25-29 years
"B12002_163E", # female, widowed, 30-34 years
"B12002_164E", # female, widowed, 35-39 years
"B12002_165E", # female, widowed, 40-44 years
"B12002_166E", # female, widowed, 45-49 years
"B12002_167E", # female, widowed, 50-54 years
"B12002_168E", # female, widowed, 55-59 years
"B12002_169E", # female, widowed, 60-64 years
"B12002_170E", # female, widowed, 65-74 years
"B12002_171E", # female, widowed, 75-84 years
"B12002_172E", # female, widowed, 85+ years
"B12002_174E", # female, divorced, 15-17 years
"B12002_175E", # female, divorced, 18-19 years
"B12002_176E", # female, divorced, 20-24 years
"B12002_177E", # female, divorced, 25-29 years
"B12002_178E", # female, divorced, 30-34 years
"B12002_179E", # female, divorced, 35-39 years
"B12002_180E", # female, divorced, 40-44 years
"B12002_181E", # female, divorced, 45-49 years
"B12002_182E", # female, divorced, 50-54 years
"B12002_183E", # female, divorced, 55-59 years
"B12002_184E", # female, divorced, 60-64 years
"B12002_185E", # female, divorced, 65-74 years
"B12002_186E", # female, divorced, 75-84 years
"B12002_187E", # female, divorced, 85+ years
# B09002 marital status by presence and age of own children under 18 years
"B09002_003E", # married-couple, child(ren) 0-2 years
"B09002_004E", # married-couple, child(ren) 3-4 years
"B09002_005E", # married-couple, child(ren) 5 years
"B09002_006E", # married-couple, child(ren) 6-11 years
"B09002_007E", # married-couple, child(ren) 12-17 years
"B09002_010E", # spdad, child(ren) 0-2 years
"B09002_011E", # spdad, child(ren) 3-4 years
"B09002_012E", # spdad, child(ren) 5 years
"B09002_013E", # spdad, child(ren) 6-11 years
"B09002_014E", # spdad, child(ren) 12-17 years
"B09002_016E", # spmom, child(ren) 0-2 years
"B09002_017E", # spmom, child(ren) 3-4 years
"B09002_018E", # spmom, child(ren) 5 years
"B09002_019E", # spmom, child(ren) 6-11 years
"B09002_020E" # spmom, child(ren) 12-17 years
# Create function-get the data
get_acs_data <- function(state, year, variables, survey) {
get_acs(
geography = ifelse(state == "US", "us", "state"),
variables = variables,
survey = survey,
state = ifelse(state == "US", NULL, state), # If state is "US", set state-NULL
year = year
)
}
# Load fips_codes data
data(fips_codes, package = "tidycensus")
# Create the list of desired states for output
states <- unique(fips_codes$state)[01:51]
states <- c(states, "US") # Add "US"-the list of states-get national totals
# Pull data for each state and year in a tryCatch block-skip unavailable
# state-year combinations)
acs_data <- lapply(states, function(state) {
lapply(years, function(year) {
tryCatch({
data <- get_acs_data(state, year, variables, survey)
data <- data %>% mutate(year = year) # Add year column-the data
}, error = function(e) {
message(paste("Skipping state", state, "year", year, "due-error:", e$message))
data.frame()
})
}) %>%
bind_rows()
}) %>%
bind_rows() # Combine the data into a single data frame