tidycensus Examples

Here’s an example of getting State Population Estimates from the 2020 Decennial Census using the tidycensus package:

library(dplyr)
library(tidycensus)

pop2020 <- get_decennial(
  geography = "state",
  variables = "P1_001N",
  year = 2020) %>%
  mutate(year = 2020, variable = "population")

pop2020
# A tibble: 52 × 5
   GEOID NAME                 variable      value  year
   <chr> <chr>                <chr>         <dbl> <dbl>
 1 42    Pennsylvania         population 13002700  2020
 2 06    California           population 39538223  2020
 3 54    West Virginia        population  1793716  2020
 4 49    Utah                 population  3271616  2020
 5 36    New York             population 20201249  2020
 6 11    District of Columbia population   689545  2020
 7 02    Alaska               population   733391  2020
 8 12    Florida              population 21538187  2020
 9 45    South Carolina       population  5118425  2020
10 38    North Dakota         population   779094  2020
# ℹ 42 more rows

Here is how you can see the states with the largest population:

arrange(pop2020, desc(value))
# A tibble: 52 × 5
   GEOID NAME           variable      value  year
   <chr> <chr>          <chr>         <dbl> <dbl>
 1 06    California     population 39538223  2020
 2 48    Texas          population 29145505  2020
 3 12    Florida        population 21538187  2020
 4 36    New York       population 20201249  2020
 5 42    Pennsylvania   population 13002700  2020
 6 17    Illinois       population 12812508  2020
 7 39    Ohio           population 11799448  2020
 8 13    Georgia        population 10711908  2020
 9 37    North Carolina population 10439388  2020
10 26    Michigan       population 10077331  2020
# ℹ 42 more rows