################################################################################################################################################################# # Own Children by Family Type # ACS Tables: B09001, B09002 ################################################################################################################################################################# # Call the acs package for data processing library(acs) # Variables to specify end year of ACS to pull from, as well as the span, which specifies the estimates to pull for that end year (1-year, 3-year, or 5-year), and the geographic level year_endyear = 2015 year_span = 5 # 1-year estimates are not available at the tract or block group level fileGeo = "County" # Additional geography fields for output countyFIPS = "" countyName = "" stateFIPS = "" stateAbbr = "" if (fileGeo == "BlockGroup") { # Block Group Geographies mo.geo=geo.make(state=29, county=c(3,13,21,25,37,47,49,63,95,101,107,165,177), tract="*", block.group="*") ks.geo=geo.make(state=20, county=c(1,5,43,45,59,91,103,107,121,209), tract="*", block.group="*") # Combine the Missouri and Kansas geographies, so we're pulling all data in a single request geo = mo.geo + ks.geo } else if (fileGeo == "Tract") { # Tract Geographies: mo.geo=geo.make(state=29, county=c(3,13,21,25,37,47,49,63,95,101,107,165,177), tract="*") ks.geo=geo.make(state=20, county=c(1,5,43,45,59,91,103,107,121,209), tract="*") # Combine the Missouri and Kansas geographies, so we're pulling all data in a single request geo = mo.geo + ks.geo } else if (fileGeo == "Place") { # Place Geographies: mo.geo=geo.make(state=29, place="*") ks.geo=geo.make(state=20, place="*") # Combine the Missouri and Kansas geographies, so we're pulling all data in a single request geo = mo.geo + ks.geo } else if (fileGeo == "County") { # County Geographies: # Top 2 are for counties in the MSA, plus counties needed for REACH data (Allen County, KS and Lafayette County, MO)] # Bottom 2 are the counties in the MARC region only #All Counties in the CSA (??) (plus Allen County, KS) if (year_span == 5){ mo.geo=geo.make(state=29, county=c(3,13,21,25,37,47,49,63,95,101,107,165,177)) ks.geo=geo.make(state=20, county=c(1,5,43,45,59,91,103,107,121,209)) } else { #Counties Available within the 1-Year ACS Estimates mo.geo=geo.make(state=29, county=c(21,37,47,95,165)) ks.geo=geo.make(state=20, county=c(45,91,103,209)) } # Combine the Missouri and Kansas geographies, so we're pulling all data in a single request geo = mo.geo + ks.geo } else if (fileGeo == "MSA") { # MSA Geographies - cannot be pulled by County or State, so getting all geo <- geo.make(msa="*") # Set the stateFIPS so that data appends correctly stateFIPS = 99 } # Clean up variables that are no longer needed. if (fileGeo != "MSA") { rm(mo.geo, ks.geo) } # Set up acs.lookup objects for data we want to retrieve population.under18 = acs.lookup(table.name = "B09001", span=year_span, endyear = year_endyear) ownChildren.byFamilyType = acs.lookup(table.name="B09002", span=year_span, endyear=year_endyear) # Select out just those variables we want population.under18.vars = population.under18[1:10] ownchildren.byfamilytype.vars = ownChildren.byFamilyType[1:20] children.vars = population.under18.vars + ownchildren.byfamilytype.vars # Request/fetch the data, and then add to a dataframe for output children.data = acs.fetch(endyear=year_endyear, span=year_span, geography=geo, variable=children.vars, dataset="acs") # Create a complete FIPS code ID for the block group, tract, place, or county - this is what is used to join this data to geography if (fileGeo == "Tract") { myFIPS = paste(geography(children.data)$state, sprintf("%03d",geography(children.data)$county), geography(children.data)$tract, sep="") } else if (fileGeo == "County") { myFIPS = paste(geography(children.data)$state, sprintf("%03s",geography(children.data)$county), sep="") } else if (fileGeo == "Place") { myFIPS = paste(geography(children.data)$state, sprintf("%05s",geography(children.data)$place), sep="") } else if (fileGeo == "BlockGroup") { myFIPS = paste(geography(children.data)$state, sprintf("%03d",geography(children.data)$county), sprintf("%06d",geography(children.data)$tract), geography(children.data)$blockgroup, sep="") } else if (fileGeo == "MSA") { myFIPS = geography(children.data)$metropolitanstatisticalareamicropolitanstatisticalarea } if (fileGeo != "MSA" && fileGeo != "Place") { countyFIPS = paste(geography(children.data)$state, sprintf("%03s",geography(children.data)$county), sep="") stateFIPS = geography(children.data)$state } else if(fileGeo != "MSA") { stateFIPS = geography(children.data)$state } children.df = data.frame(geography(children.data)$NAME, myFIPS, countyFIPS, countyName, stateFIPS, stateAbbr, year_endyear, year_span, estimate(children.data)) # Overwrite default ACS field names with English cols_children = c("GeoStr", "FIPS", "CountyFIPS", "CountyName", "StateFIPS", "StateAbbr", "Period", "Dataset", "PopulationUnder18", "Under18InHouseholds", "AgeUnder3Years", "Age3to4", "Age5", "Age6to8", "Age9to11", "Age12to14", "Age15to17", "Under18InGroupQuarters", "OwnChildrenUnder18", "InMarriedCouples","InMarriedCouples_Under3years","InMarriedCouples_3to4years","InMarriedCouples_5years","InMarriedCouples_6to11years","InMarriedCouples_12to17years","InOtherFamilies","MaleHoH","MaleHoH_Under3years","MaleHoH_3to4years","MaleHoH_5years","MaleHoH_6to11years","MaleHoH_12to17years","FemaleHoH","FemaleHoH_Under3years","FemaleHoH_3to4years","FemaleHoH_5years","FemaleHoH_6to11years","FemaleHoH_12to17years") colnames(children.df)=cols_children # Set rownames to NULL, so it defaults to a sequential numeric output rownames(children.df) <- NULL #Clean up unneeded variables rm(ownChildren.byFamilytype, ownchildren.byfamilytype.vars, population.under18, population.under18.vars, children.vars, children.data, children.df)