Written and maintained by CASRAI Editorial Board
Published
On this page
tidycensus is an R package, created by Kyle Walker and Matt Herman, that downloads United States Census Bureau data directly into R as tidyverse-ready data frames, optionally with simple feature geometry for mapping. Its main functions are get_decennial() for the 2000, 2010 and 2020 decennial censuses and get_acs() for the 1-year and 5-year American Community Survey, and it needs a free Census API key before any call will work. This guide covers installation, a first working query, how to read the output (including margins of error), and the errors researchers most often hit, using the package documentation for version 2.0.0.
What tidycensus is used for in research
tidycensus is used to pull population, housing and socioeconomic figures for named geographies (state, county, Census tract and others) straight from the Census Bureau’s APIs, so an analysis script can regenerate its own input data instead of relying on manually downloaded spreadsheets. The package documentation describes its intent as returning population characteristics of the United States in tidy format that integrates with simple feature geometries. It is deliberately not a wrapper for every Census Bureau API: the author points users to the censusapi package for datasets tidycensus does not include and to the lehdr package for LODES data.
Beyond the decennial census and the ACS, tidycensus includes a select number of other Census Bureau products: the Population Estimates API through get_estimates() and the Migration Flows API. Individual-level ACS microdata (PUMS) is available through get_pums(). For background on the survey itself, see the CASRAI guide to American Community Survey data, and for the individual-record files see ACS Public Use Microdata Sample (PUMS).
Two features matter for research reproducibility. First, results arrive as a tibble in a tidy layout, one row per geography and variable, which suits the data-reshaping workflow described in the guide to tidyr. Second, setting geometry = TRUE returns an sf tibble that carries boundaries, which are the same Census geography products described in the guide to TIGER/Line shapefiles.
How to install or access tidycensus
Install tidycensus from CRAN, the official R package repository, then obtain a Census API key. The package website (walker-data.com/tidycensus) gives the install command as shown below, and the CRAN reference manual lists version 2.0.0, dated 2026-10-16.
- In R or RStudio, run
install.packages("tidycensus"). - Request a free key at the Census Bureau’s key signup page (api.census.gov/data/key_signup.html). The form asks for an organization name and an email address and requires agreement to the terms of service.
- Load the package and store the key once with
census_api_key("YOUR API KEY GOES HERE", install = TRUE).
The key is not optional. The basic-usage article states that the Census Bureau no longer accepts unauthenticated requests, so tidycensus calls will error out when no key is available. With install = TRUE the key is written to your .Renviron file and is picked up automatically in later R sessions, so you only do this once. Alternatively, pass key = "YOUR KEY" directly to any tidycensus function. The reference manual gives the signature as census_api_key(key, install = FALSE, overwrite = FALSE), so set overwrite = TRUE if you need to replace a key that was saved earlier.
For a shared project, keep the key out of the scripts you publish: the .Renviron route means the analysis code itself contains no secret, and a collaborator only needs to run the same one-time census_api_key() call with their own key.
How to download Census and ACS data with tidycensus step by step
The workflow has four steps: load the packages, find the variable code, request the data, and then check the result. The examples below are from the package’s basic-usage and margins-of-error articles.
Step 1: load the packages
library(tidycensus)
library(tidyverse)
Step 2: find the variable you need
Census tables are addressed by variable codes such as B19013_001. Use load_variables() with a year and dataset to browse them. The documentation’s example is v24 = load_variables(2024, "acs5") followed by View(v24) to search the list in RStudio. Valid dataset names include “pl”, “dhc”, “acs1” and “acs5”, and for the ACS you can add the suffixes /profile, /subject and /cprofile to reach the data profile, subject and comparison profile tables.
Step 3: request the data
A decennial query asks for a variable, a geography, a year and a summary file. The documentation’s example retrieves median age by state from the 2020 Demographic and Housing Characteristics file:
age20 = get_decennial(geography = "state", variables = "P13_001N", year = 2020, sumfile = "dhc")
An ACS query follows the same pattern. The documentation’s example retrieves median household income for counties in Vermont, with a label given to the variable:
vt = get_acs(geography = "county", variables = c(medincome = "B19013_001"), state = "VT", year = 2024)
For ACS data, year = 2024 with the default 5-year survey means the 2020 to 2024 estimates. To use 1-year estimates instead, set survey = "acs1"; the reference manual also lists “acs3” and “acs5” as options, and “acs5” is the default.
Step 4: add geometry, population estimates or microdata when needed
Set geometry = TRUE in get_acs() to receive an sf tibble that can be mapped; the default is FALSE. For annual population estimates, call get_estimates() with a product and a vintage; the documentation’s example requests the “components” product for US states in vintage 2024. For person and housing-unit records, get_pums() defaults to the year 2024, accepts survey = "acs1" or “acs5”, and can add replicate weights with rep_weights set to “person”, “housing” or “both”. The data table below compares what each function covers and how it defaults.
How to read the tidycensus output
Read the output by checking the column layout first: tidycensus returns a tibble in tidy form by default, with one row per geography and variable. For get_decennial() the default columns are GEOID, NAME, variable and value. For get_acs() the value column is replaced by estimate and moe, the margin of error. In the 2020 median-age example, the first rows show Connecticut and Delaware at 41.1 and the District of Columbia at 33.9.
Tidy versus wide output
With output = "wide", each variable becomes its own column instead of its own row, which is convenient when you need several variables side by side for each geography. Naming variables in the request, as in c(medincome = "B19013_001"), gives those columns readable names.
Margins of error in the ACS
ACS estimates are sample-based and each carries a margin of error, so treat the moe column as part of the result rather than an extra. The package’s default confidence level for the margin of error is 90 percent; set moe_level to 95 or 99 to change it. The margins-of-error article makes the practical point with Census tracts in Ramsey County, Minnesota (2020-2024 ACS): across the 1,716-row dataset of age-by-sex cells, the margin of error exceeded the estimate in roughly 43 percent of rows. Its suggested remedy is aggregation: the functions moe_sum(), moe_prop(), moe_ratio() and moe_product() implement the Census Bureau’s formulas for derived estimates. The Census Bureau itself warns that these methods do not account for covariance between the basic estimates, so the approximated standard error may be over- or under-stated.
Counts versus estimates
The decennial census and the ACS answer different questions. The documentation quotes the Census Bureau: the main function of the decennial census is to count people for apportionment and redistricting, while the primary purpose of the ACS is to measure changing social and economic characteristics, so the ACS does not provide official population counts between censuses. Report ACS values as estimates with their margins of error and avoid presenting them as exact counts.
Population estimate products
In get_estimates() the products are “population”, “components”, “housing”, “characteristics” and “intercensal”. The housing product is not available for 2020 and later. The components product returns both counts and rates; variable names beginning with R are rates per 1,000 residents, for example RBIRTH and RNETMIG.
Troubleshooting tidycensus
- The call fails with a key error. The Census Bureau requires a key. Run
census_api_key()as described above, or passkey =to the function, then restart R if you saved it withinstall = TRUEso the new.Renvironvalue is read. - The wrong decennial file is queried. When
sumfileis left at its default (NULL),get_decennial()uses “pl” for 2020 and “sf1” for 2000 and 2010. Variable IDs repeat across files, so name the file explicitly, such assumfile = "dhc", when you want that file’s table. - The ACS period is not what you expected. With the default 5-year survey the
yearargument is the last year of the period, so 2024 returns 2020 to 2024. - Margins of error look too large. Small geographies and small cells have large margins. Aggregate the cells and use
moe_sum()or the related functions, as above. - Estimates product is missing for a year. The housing product is not available for 2020 and later, and the reference manual notes that housing estimates need the readxl package for Vintage 2021 and later.
- A warning mentions
cache_table. The reference manual marks this argument as deprecated and ignored, so remove it from older scripts.
When a table or variable does not seem to exist, confirm the code against load_variables() for the same year and dataset before assuming the package is at fault, because table definitions change between survey years.
Free alternatives to tidycensus
Each alternative below fits a different need. The comparison table is limited to what each project’s own page says it does.
| Tool | What its own documentation says it does | Language or interface |
|---|---|---|
| pytidycensus | A Python port of tidycensus: an interface to several Census Bureau APIs and boundary files that returns Census and ACS data as pandas DataFrames, optionally GeoPandas GeoDataFrames. | Python |
| censusapi | Recommended in the tidycensus documentation for access to all Census Bureau APIs, including datasets tidycensus does not wrap. | R |
| lehdr | Recommended in the tidycensus documentation for Census Bureau LODES data. | R |
| IPUMS NHGIS | Provides summary tables and time series of population, housing and other U.S. Census data, with mapping files, and has an API with access in R and Python. | Web, R, Python |
None of these replaces the need to understand the underlying survey. If your project needs consistent tables across many decades, the NHGIS time series are built for that, whereas tidycensus is organised around the current Census Bureau APIs.
Sources
Data: tidycensus data functions: Census Bureau data covered, key arguments and defaults
Compares the four main data-retrieval functions of tidycensus 2.0.0 (get_decennial, get_acs, get_estimates, get_pums) by the Census Bureau data they return, their key arguments and their documented defaults, compiled from the package website articles and the CRAN reference manual.
Period covered: 2000/2024. Coverage: United States.
| Function | Census Bureau data | Key arguments | Documented defaults and limits |
|---|---|---|---|
| get_decennial() | Decennial census, 2000, 2010 and 2020 | geography, variables, year, sumfile | sumfile NULL defaults to pl for 2020 and sf1 for 2000 and 2010; tidy output with GEOID, NAME, variable, value |
| get_acs() | American Community Survey, 1-year and 5-year | geography, variables, year, survey, moe_level, geometry, summary_var | survey acs5; moe_level 90; geometry FALSE; tidy output with estimate and moe |
| get_estimates() | Population Estimates Program | geography, product, vintage, variables | products population, components, housing, characteristics, intercensal; housing not available for 2020 and later |
| get_pums() | ACS Public Use Microdata Sample | variables, state, year, survey, recode, rep_weights | year 2024; survey acs1 or acs5; rep_weights person, housing or both |
Sources
- Basic usage of tidycensus (accessed 2026-12-03)
- tidycensus reference manual, version 2.0.0 (accessed 2026-12-03)
- Other Census Bureau datasets (tidycensus) (accessed 2026-12-03)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







