# Get Census Data - R Code # Set Census API Key Sys.setenv(CENSUS_KEY="YOUR API KEY. GET API KEY HERE: https://api.census.gov/data/key_signup.html") Sys.getenv("CENSUS_KEY") # Install these libraries # censusapi for calling the available APIs # data.table is a personal preference that is my preferred data frame tool. YMMV # checks if required packages are available (censusapi, data.table in our case) and if not available installs them list.of.packages <- c("censusapi", "data.table") new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages) # Loads individual packages to be available for R Session library(censusapi) library(data.table) # List all available datasets apis <- listCensusApis() # Example: Get Workforce Data for all counties in Georgia # Get values from individual tables # Table Shells are here: https://www.census.gov/programs-surveys/acs/technical-documentation/table-shells.2017.html # Code Description #setDT formats the dataframe as data.table object # getCensus is the call to get the Census tables from the censusapi library # name=acs/acs5, vintage=2017 - Calls for 2017 5 year ACS data # vars=c("NAME","C23002A_001E",etc.) - calls the tables you want # region = "county:*", regionin = "state:13" - Calls all counties in Georgia householdwforce_County <- setDT(getCensus(name = "acs/acs5", vintage = 2017, vars = c("NAME", "C23002A_001E","C23002A_002E","C23002A_003E","C23002A_004E","C23002A_005E","C23002A_006E","C23002A_007E","C23002A_008E","C23002A_009E","C23002A_010E","C23002A_011E","C23002A_012E","C23002A_013E","C23002A_014E","C23002A_015E","C23002A_016E","C23002A_017E","C23002A_018E","C23002A_019E","C23002A_020E","C23002A_021E","C23002A_022E","C23002A_023E","C23002A_024E","C23002A_025E","C23002A_026E","C23002A_027E","C23002B_001E","C23002B_002E","C23002B_003E","C23002B_004E","C23002B_005E","C23002B_006E","C23002B_007E","C23002B_008E","C23002B_009E","C23002B_010E","C23002B_011E","C23002B_012E","C23002B_013E","C23002B_014E","C23002B_015E","C23002B_016E","C23002B_017E","C23002B_018E","C23002B_019E","C23002B_020E","C23002B_021E","C23002B_022E","C23002B_023E","C23002B_024E","C23002B_025E","C23002B_026E","C23002B_027E","C23002C_001E","C23002C_002E","C23002C_003E","C23002C_004E","C23002C_005E","C23002C_006E","C23002C_007E","C23002C_008E","C23002C_009E","C23002C_010E","C23002C_011E","C23002C_012E","C23002C_013E","C23002C_014E","C23002C_015E","C23002C_016E","C23002C_017E","C23002C_018E","C23002C_019E","C23002C_020E","C23002C_021E","C23002C_022E","C23002C_023E","C23002C_024E","C23002C_025E","C23002C_026E","C23002C_027E","C23002D_001E","C23002D_002E","C23002D_003E","C23002D_004E","C23002D_005E","C23002D_006E","C23002D_007E","C23002D_008E","C23002D_009E","C23002D_010E","C23002D_011E","C23002D_012E","C23002D_013E","C23002D_014E","C23002D_015E","C23002D_016E","C23002D_017E","C23002D_018E","C23002D_019E","C23002D_020E","C23002D_021E","C23002D_022E","C23002D_023E","C23002D_024E","C23002D_025E","C23002D_026E","C23002D_027E","C23002E_001E","C23002E_002E","C23002E_003E","C23002E_004E","C23002E_005E","C23002E_006E","C23002E_007E","C23002E_008E","C23002E_009E","C23002E_010E","C23002E_011E","C23002E_012E","C23002E_013E","C23002E_014E","C23002E_015E","C23002E_016E","C23002E_017E","C23002E_018E","C23002E_019E","C23002E_020E","C23002E_021E","C23002E_022E","C23002E_023E","C23002E_024E","C23002E_025E","C23002E_026E","C23002E_027E","C23002F_001E","C23002F_002E","C23002F_003E","C23002F_004E","C23002F_005E","C23002F_006E","C23002F_007E","C23002F_008E","C23002F_009E","C23002F_010E","C23002F_011E","C23002F_012E","C23002F_013E","C23002F_014E","C23002F_015E","C23002F_016E","C23002F_017E","C23002F_018E","C23002F_019E","C23002F_020E","C23002F_021E","C23002F_022E","C23002F_023E","C23002F_024E","C23002F_025E","C23002F_026E","C23002F_027E","C23002G_001E","C23002G_002E","C23002G_003E","C23002G_004E","C23002G_005E","C23002G_006E","C23002G_007E","C23002G_008E","C23002G_009E","C23002G_010E","C23002G_011E","C23002G_012E","C23002G_013E","C23002G_014E","C23002G_015E","C23002G_016E","C23002G_017E","C23002G_018E","C23002G_019E","C23002G_020E","C23002G_021E","C23002G_022E","C23002G_023E","C23002G_024E","C23002G_025E","C23002G_026E","C23002G_027E","C23002H_001E","C23002H_002E","C23002H_003E","C23002H_004E","C23002H_005E","C23002H_006E","C23002H_007E","C23002H_008E","C23002H_009E","C23002H_010E","C23002H_011E","C23002H_012E","C23002H_013E","C23002H_014E","C23002H_015E","C23002H_016E","C23002H_017E","C23002H_018E","C23002H_019E","C23002H_020E","C23002H_021E","C23002H_022E","C23002H_023E","C23002H_024E","C23002H_025E","C23002H_026E","C23002H_027E","C23002I_001E","C23002I_002E","C23002I_003E","C23002I_004E","C23002I_005E","C23002I_006E","C23002I_007E","C23002I_008E","C23002I_009E","C23002I_010E","C23002I_011E","C23002I_012E","C23002I_013E","C23002I_014E","C23002I_015E","C23002I_016E","C23002I_017E","C23002I_018E","C23002I_019E","C23002I_020E","C23002I_021E","C23002I_022E","C23002I_023E","C23002I_024E","C23002I_025E","C23002I_026E","C23002I_027E" ), region = "county:*", regionin = "state:13")) # This adds a new column of FIPS codes for the counties householdwforce_County[,fips := as.numeric(as.character(state))*1000+as.numeric(as.character(county))]