Skip to contents

Retrieve Census data from a given API

Usage

getCensus(
  name,
  vintage = NULL,
  key = Sys.getenv("CENSUS_KEY"),
  vars,
  region = NULL,
  regionin = NULL,
  time = NULL,
  year = NULL,
  date = NULL,
  period = NULL,
  monthly = NULL,
  show_call = FALSE,
  convert_variables = TRUE,
  category_code = NULL,
  data_type_code = NULL,
  naics = NULL,
  pscode = NULL,
  naics2012 = NULL,
  naics2007 = NULL,
  naics2002 = NULL,
  naics1997 = NULL,
  sic = NULL,
  ...
)

Arguments

name

The programmatic name of your dataset, e.g. `timeseries/poverty/saipe` or `acs/acs5`. See `listCensusApis()` for options.

vintage

Vintage (year) of dataset, e.g. 2014. Not required for timeseries APIs.

key

Your Census API key, obtained at https://api.census.gov/data/key_signup.html. This function will default to a `CENSUS_KEY` stored in your .Renviron if available.

vars

List of variables to get. Required.

region

Geography to get.

regionin

Optional hierarchical geography to limit region.

time, year, date, period, monthly

Optional arguments used for some time series APIs.

show_call

List the underlying API call that was sent to the Census Bureau.

convert_variables

Convert likely numeric variables into numeric data. Default is true. If false, results will be characters, which is the type returned by the Census Bureau.

category_code, data_type_code, naics, pscode, naics2012, naics2007, naics2002, naics1997, sic

Optional arguments used in economic data APIs.

...

Other valid arguments to pass to the Census API. Note: the APIs are case sensitive.

Examples

# \dontrun{
# Get total population and median household income for places (cities, towns, villages)
# in one state from the 5-year ACS.
acs_simple <- getCensus(
  name = "acs/acs5",
  vintage = 2020,
  vars = c("NAME", "B01001_001E", "B19013_001E"),
  region = "place:*",
  regionin = "state:01")
head(acs_simple)
#>   state place                         NAME B01001_001E B19013_001E
#> 1    01 44536    Lowndesboro town, Alabama          75       41667
#> 2    01 44608         Loxley town, Alabama        2767       62292
#> 3    01 44728        Luverne city, Alabama        2742       48468
#> 4    01 44800           Lynn town, Alabama         832       36115
#> 5    01 45040         McCalla CDP, Alabama       12077       71061
#> 6    01 45208 McDonald Chapel CDP, Alabama         662       49038

# Get all data from the B19013 variable group.
# This returns estimates as well as margins of error and annotation flags.
acs_group <- getCensus(
  name = "acs/acs5",
  vintage = 2020,
  vars = c("B01001_001E", "group(B19013)"),
  region = "place:*",
  regionin = "state:01")
head(acs_group)
#>   state place B01001_001E B19013_001E B19013_001EA B19013_001M B19013_001MA
#> 1    01 44536          75       41667         <NA>       27339         <NA>
#> 2    01 44608        2767       62292         <NA>       17233         <NA>
#> 3    01 44728        2742       48468         <NA>       10129         <NA>
#> 4    01 44800         832       36115         <NA>        8289         <NA>
#> 5    01 45040       12077       71061         <NA>        3184         <NA>
#> 6    01 45208         662       49038         <NA>       11453         <NA>
#>             GEO_ID                         NAME
#> 1 1600000US0144536    Lowndesboro town, Alabama
#> 2 1600000US0144608         Loxley town, Alabama
#> 3 1600000US0144728        Luverne city, Alabama
#> 4 1600000US0144800           Lynn town, Alabama
#> 5 1600000US0145040         McCalla CDP, Alabama
#> 6 1600000US0145208 McDonald Chapel CDP, Alabama

# Retreive 2010 Decennial Census block-level data within a specific tract,
# using the regionin argument to precisely specify the Census tract.
decennial_2010 <- getCensus(
  name = "dec/sf1",
  vintage = 2010,
  vars = c("NAME","P001001"),
  region = "block:*",
  regionin = "state:36+county:027+tract:010000")
head(decennial_2010)
#>   state county  tract block
#> 1    36    027 010000  1000
#> 2    36    027 010000  1011
#> 3    36    027 010000  1028
#> 4    36    027 010000  1001
#> 5    36    027 010000  1031
#> 6    36    027 010000  1002
#>                                                                     NAME
#> 1 Block 1000, Block Group 1, Census Tract 100, Dutchess County, New York
#> 2 Block 1011, Block Group 1, Census Tract 100, Dutchess County, New York
#> 3 Block 1028, Block Group 1, Census Tract 100, Dutchess County, New York
#> 4 Block 1001, Block Group 1, Census Tract 100, Dutchess County, New York
#> 5 Block 1031, Block Group 1, Census Tract 100, Dutchess County, New York
#> 6 Block 1002, Block Group 1, Census Tract 100, Dutchess County, New York
#>   P001001
#> 1      31
#> 2      17
#> 3      41
#> 4       0
#> 5       0
#> 6       4

# Get poverty rates for children and for people of all ages over time using the
# Small Area Income and Poverty Estimates API
saipe <- getCensus(
  name = "timeseries/poverty/saipe",
  vars = c("NAME", "SAEPOVRT0_17_PT", "SAEPOVRTALL_PT"),
  region = "state:01",
  year = "2000:2019")
head(saipe)
#>   state    NAME SAEPOVRT0_17_PT SAEPOVRTALL_PT YEAR
#> 1    01 Alabama            20.5           14.6 2000
#> 2    01 Alabama            22.1           15.7 2001
#> 3    01 Alabama            21.6           15.4 2002
#> 4    01 Alabama            22.3           15.3 2003
#> 5    01 Alabama            22.6           16.1 2004
#> 6    01 Alabama            24.3           16.9 2005

# Get County Business Patterns data for a specific NAICS sector.
cbp_2016 <- getCensus(
 name = "cbp",
  vintage = "2016",
  vars = c("EMP", "ESTAB", "NAICS2012_TTL", "GEO_TTL"),
  region = "state:*",
  naics2012 = "23")
head(cbp_2016)
#>   state    EMP ESTAB NAICS2012_TTL       GEO_TTL NAICS2012
#> 1    27 118033 16360  Construction     Minnesota        23
#> 2    28  44551  3796  Construction   Mississippi        23
#> 3    29 121494 13263  Construction      Missouri        23
#> 4    25 131693 18618  Construction Massachusetts        23
#> 5    26 138920 18803  Construction      Michigan        23
#> 6    30  25036  5326  Construction       Montana        23
# }