Accessing Census Data in R through APIs

Stephanie Zimmer

Overview

Census API

  • What is the Census API?
    • An API that gives access to data from many programs
  • Programs include:
    • American Community Survey: tables and microdata
    • Decennial Census
    • Population Estimates and Projections
    • And more
  • Census documentation

Useful R packages

  • censusapi
    • Any data program available on Census API
    • Data is not cleaned
    • Not discussed today
  • tidycensus
    • American Community Survey (ACS) tables, ACS microdata, Decennial tables, PEP tables, and ACS migration flow data
    • Nice wrappers
    • Data has special missing codes changed to NA

API keys

  • An API key is needed and needs to be requested1

  • Bad practice to store an API key in your code

  • Install the API key locally to your .Renviron file

  • Only need to do this per device you use

tidycensus::census_api_key("abcdefghijkl", overwrite = TRUE, install = TRUE)

Retrieve using:

Sys.getenv("CENSUS_API_KEY")

Tabular data from ACS

Workflow for getting tabular estimates

  1. Find the variables using load_variables()
  2. Use get_acs(), get_decennial(), get_flows(), or get_estimates() to query the data

Motivating example

Find number of housing units by where current college students live in each block group in Durham County, NC from ACS 2019-2023 data

year_select <- 2023

Find variables

  • Looking at 5-year ACS data
library(tidycensus)
library(sf)
findvars_tidy <- load_variables(year = year_select, dataset = "acs5")
glimpse(findvars_tidy)
Rows: 28,261
Columns: 4
$ name      <chr> "B01001A_001", "B01001A_002", "B01001A_003", "B01001A_004", …
$ label     <chr> "Estimate!!Total:", "Estimate!!Total:!!Male:", "Estimate!!To…
$ concept   <chr> "Sex by Age (White Alone)", "Sex by Age (White Alone)", "Sex…
$ geography <chr> "tract", "tract", "tract", "tract", "tract", "tract", "tract…

Find variables

  • Looking at 5-year ACS data for School Enrollment variables available at block group level
  • After manually exploring the data, determine we want the variables from the concept “School Enrollment by Detailed Level of School for the Population 3 Years and Over” (B14007)
findvars_tidy %>%
  filter(
    str_detect(concept, "School Enrollment"),
    geography == "block group"
  ) %>%
  mutate(Table = word(name, 1, sep = "_")) %>%
  count(Table, concept)
# A tibble: 13 × 3
   Table   concept                                                             n
   <chr>   <chr>                                                           <int>
 1 B14002  Sex by School Enrollment by Level of School by Type of School …    49
 2 B14005  Sex by School Enrollment by Educational Attainment by Employme…    29
 3 B14007  School Enrollment by Detailed Level of School for the Populati…    19
 4 B14007A School Enrollment by Detailed Level of School for the Populati…    19
 5 B14007B School Enrollment by Detailed Level of School for the Populati…    19
 6 B14007C School Enrollment by Detailed Level of School for the Populati…    19
 7 B14007D School Enrollment by Detailed Level of School for the Populati…    19
 8 B14007E School Enrollment by Detailed Level of School for the Populati…    19
 9 B14007F School Enrollment by Detailed Level of School for the Populati…    19
10 B14007G School Enrollment by Detailed Level of School for the Populati…    19
11 B14007H School Enrollment by Detailed Level of School for the Populati…    19
12 B14007I School Enrollment by Detailed Level of School for the Populati…    19
13 B99141  Allocation of School Enrollment for the Population 3 Years and…     3
findvars_tidy %>%
  filter(str_detect(name, "B14007_")) %>%
  select(name, label)
# A tibble: 19 × 2
   name       label                                                             
   <chr>      <chr>                                                             
 1 B14007_001 Estimate!!Total:                                                  
 2 B14007_002 Estimate!!Total:!!Enrolled in school:                             
 3 B14007_003 Estimate!!Total:!!Enrolled in school:!!Enrolled in nursery school…
 4 B14007_004 Estimate!!Total:!!Enrolled in school:!!Enrolled in kindergarten   
 5 B14007_005 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 1        
 6 B14007_006 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 2        
 7 B14007_007 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 3        
 8 B14007_008 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 4        
 9 B14007_009 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 5        
10 B14007_010 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 6        
11 B14007_011 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 7        
12 B14007_012 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 8        
13 B14007_013 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 9        
14 B14007_014 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 10       
15 B14007_015 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 11       
16 B14007_016 Estimate!!Total:!!Enrolled in school:!!Enrolled in grade 12       
17 B14007_017 Estimate!!Total:!!Enrolled in school:!!Enrolled in college, under…
18 B14007_018 Estimate!!Total:!!Enrolled in school:!!Graduate or professional s…
19 B14007_019 Estimate!!Total:!!Not enrolled in school                          

Clean up variable labels

myvars_labeled
# A tibble: 17 × 2
   name       EducEnrollment                          
   <chr>      <chr>                                   
 1 B14007_003 Enrolled in nursery school, preschool   
 2 B14007_004 Enrolled in kindergarten                
 3 B14007_005 Enrolled in grade 1                     
 4 B14007_006 Enrolled in grade 2                     
 5 B14007_007 Enrolled in grade 3                     
 6 B14007_008 Enrolled in grade 4                     
 7 B14007_009 Enrolled in grade 5                     
 8 B14007_010 Enrolled in grade 6                     
 9 B14007_011 Enrolled in grade 7                     
10 B14007_012 Enrolled in grade 8                     
11 B14007_013 Enrolled in grade 9                     
12 B14007_014 Enrolled in grade 10                    
13 B14007_015 Enrolled in grade 11                    
14 B14007_016 Enrolled in grade 12                    
15 B14007_017 Enrolled in college, undergraduate years
16 B14007_018 Graduate or professional school         
17 B14007_019 Not enrolled in school                  

Get the data

sch_enroll_tidy <- get_acs(
  geography = "block group",
  variables = pull(myvars_labeled, name),
  year = year_select,
  survey = "acs5",
  state = "37",
  county = "063",
  geometry = TRUE
)

Get the data

sch_enroll_tidy
Simple feature collection with 4046 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -79.0163 ymin: 35.86321 xmax: -78.69985 ymax: 36.23932
Geodetic CRS:  NAD83
First 10 features:
          GEOID
1  370630018023
2  370630018023
3  370630018023
4  370630018023
5  370630018023
6  370630018023
7  370630018023
8  370630018023
9  370630018023
10 370630018023
                                                               NAME   variable
1  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_003
2  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_004
3  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_005
4  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_006
5  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_007
6  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_008
7  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_009
8  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_010
9  Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_011
10 Block Group 3; Census Tract 18.02; Durham County; North Carolina B14007_012
   estimate moe                       geometry
1         0  14 MULTIPOLYGON (((-78.86279 3...
2         1   3 MULTIPOLYGON (((-78.86279 3...
3         0  14 MULTIPOLYGON (((-78.86279 3...
4        61  65 MULTIPOLYGON (((-78.86279 3...
5        51  58 MULTIPOLYGON (((-78.86279 3...
6        11  19 MULTIPOLYGON (((-78.86279 3...
7         0  14 MULTIPOLYGON (((-78.86279 3...
8        29  47 MULTIPOLYGON (((-78.86279 3...
9        30  50 MULTIPOLYGON (((-78.86279 3...
10       33  48 MULTIPOLYGON (((-78.86279 3...

Label data

sch_enroll_tidy_lab <-
  sch_enroll_tidy %>%
  select(-NAME) %>%
  left_join(myvars_labeled, by = c("variable" = "name"))

sch_enroll_tidy_lab
Simple feature collection with 4046 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -79.0163 ymin: 35.86321 xmax: -78.69985 ymax: 36.23932
Geodetic CRS:  NAD83
First 10 features:
          GEOID   variable estimate moe                        EducEnrollment
1  370630018023 B14007_003        0  14 Enrolled in nursery school, preschool
2  370630018023 B14007_004        1   3              Enrolled in kindergarten
3  370630018023 B14007_005        0  14                   Enrolled in grade 1
4  370630018023 B14007_006       61  65                   Enrolled in grade 2
5  370630018023 B14007_007       51  58                   Enrolled in grade 3
6  370630018023 B14007_008       11  19                   Enrolled in grade 4
7  370630018023 B14007_009        0  14                   Enrolled in grade 5
8  370630018023 B14007_010       29  47                   Enrolled in grade 6
9  370630018023 B14007_011       30  50                   Enrolled in grade 7
10 370630018023 B14007_012       33  48                   Enrolled in grade 8
                         geometry
1  MULTIPOLYGON (((-78.86279 3...
2  MULTIPOLYGON (((-78.86279 3...
3  MULTIPOLYGON (((-78.86279 3...
4  MULTIPOLYGON (((-78.86279 3...
5  MULTIPOLYGON (((-78.86279 3...
6  MULTIPOLYGON (((-78.86279 3...
7  MULTIPOLYGON (((-78.86279 3...
8  MULTIPOLYGON (((-78.86279 3...
9  MULTIPOLYGON (((-78.86279 3...
10 MULTIPOLYGON (((-78.86279 3...

Calculate number in college/grad school by area

college_enroll_tidy <- sch_enroll_tidy_lab %>%
  filter(str_detect(EducEnrollment, "college|Grad")) %>%
  group_by(GEOID) %>%
  summarize(
    N = sum(estimate),
    .groups = "drop"
  )

Map of where college students live

college_enroll_tidy %>%
  ggplot(aes(fill = N)) +
  geom_sf() +
  theme_map() +
  scale_fill_viridis_c(option = "plasma") +
  theme(legend.position = "bottom")

Map of where college students live

Microdata from ACS

Workflow

  1. Identify geography of interest
  2. Identify variables of interest (pums_variables)
  3. Use get_pums() to query data

Warning - this data can be large!!!

Identify variables

pums_vars_2023 <-
  pums_variables %>%
  filter(year == year_select, survey == "acs5")

pums_vars_2023 %>% print(n = 5)
# A tibble: 5,434 × 12
  survey year  var_code var_label      data_type level val_min val_max val_label
  <chr>  <chr> <chr>    <chr>          <chr>     <chr> <chr>   <chr>   <chr>    
1 acs5   2023  SERIALNO Housing unit/… chr       <NA>  2019GQ… 2023GQ… GQ Uniqu…
2 acs5   2023  SERIALNO Housing unit/… chr       <NA>  2019HU… 2023HU… HU Uniqu…
3 acs5   2023  DIVISION Division code  chr       <NA>  0       0       Puerto R…
4 acs5   2023  DIVISION Division code  chr       <NA>  1       1       New Engl…
5 acs5   2023  DIVISION Division code  chr       <NA>  2       2       Middle A…
# ℹ 5,429 more rows
# ℹ 3 more variables: recode <lgl>, val_length <int>, val_na <dbl>

Get some microdata

  • For Durham, we want PUMAs1 37 01301 and 37 01302 (Durham County South and North, respectively)2
pums_recoded <- get_pums(
  variables = c("PUMA", "SEX", "AGEP", "SCHL", "SCHG"),
  state = "NC",
  puma = c("01301", "01302"),
  survey = "acs5",
  year = year_select,
  recode = TRUE,
  rep_weights = "person"
  )

Glimpse the microdata

glimpse(pums_recoded)
Rows: 12,665
Columns: 94
$ SERIALNO    <chr> "2022HU0591126", "2022HU0591126", "2022HU0594386", "2022HU…
$ SPORDER     <dbl> 1, 2, 1, 2, 1, 2, 1, 2, 3, 1, 1, 1, 2, 3, 4, 5, 6, 1, 2, 1…
$ AGEP        <dbl> 80, 77, 51, 60, 72, 22, 51, 48, 20, 54, 24, 53, 53, 16, 16…
$ PUMA        <chr> "01302", "01302", "01301", "01301", "01302", "01302", "013…
$ STATE       <chr> "37", "37", "37", "37", "37", "37", "37", "37", "37", "37"…
$ SCHG        <chr> "bb", "bb", "bb", "bb", "bb", "bb", "bb", "bb", "15", "bb"…
$ SCHL        <chr> "18", "19", "21", "21", "06", "19", "09", "15", "20", "17"…
$ SEX         <chr> "1", "2", "1", "1", "2", "2", "1", "2", "2", "2", "2", "2"…
$ STATE_label <ord> North Carolina/NC, North Carolina/NC, North Carolina/NC, N…
$ SCHG_label  <ord> N/A (not attending school), N/A (not attending school), N/…
$ SCHL_label  <ord> "Some college, but less than 1 year", "1 or more years of …
$ SEX_label   <ord> Male, Female, Male, Male, Female, Female, Male, Female, Fe…
$ WGTP        <dbl> 13, 13, 15, 15, 20, 20, 16, 16, 16, 46, 15, 9, 9, 9, 9, 9,…
$ PWGTP       <dbl> 13, 13, 15, 28, 20, 7, 16, 12, 9, 46, 15, 8, 20, 12, 13, 1…
$ PWGTP1      <dbl> 15, 13, 23, 46, 30, 5, 23, 18, 10, 57, 27, 16, 38, 26, 19,…
$ PWGTP2      <dbl> 12, 12, 21, 31, 18, 7, 20, 24, 14, 15, 5, 4, 8, 3, 4, 4, 5…
$ PWGTP3      <dbl> 4, 4, 6, 8, 12, 3, 26, 17, 19, 70, 21, 3, 5, 4, 5, 5, 5, 5…
$ PWGTP4      <dbl> 17, 17, 23, 48, 30, 12, 13, 17, 9, 41, 23, 8, 18, 9, 16, 1…
$ PWGTP5      <dbl> 20, 20, 16, 25, 27, 16, 5, 4, 4, 14, 30, 10, 18, 8, 9, 15,…
$ PWGTP6      <dbl> 3, 4, 29, 45, 33, 11, 17, 16, 7, 16, 17, 11, 20, 15, 15, 1…
$ PWGTP7      <dbl> 5, 5, 19, 27, 40, 11, 15, 18, 13, 56, 14, 20, 37, 18, 29, …
$ PWGTP8      <dbl> 26, 24, 4, 8, 6, 2, 23, 17, 12, 55, 18, 9, 20, 15, 13, 14,…
$ PWGTP9      <dbl> 12, 13, 12, 22, 43, 6, 14, 10, 8, 43, 16, 3, 5, 3, 3, 3, 4…
$ PWGTP10     <dbl> 12, 12, 4, 10, 19, 6, 23, 20, 13, 55, 22, 10, 23, 10, 19, …
$ PWGTP11     <dbl> 25, 23, 20, 38, 44, 14, 18, 11, 11, 49, 3, 3, 7, 4, 4, 5, …
$ PWGTP12     <dbl> 14, 14, 14, 26, 5, 2, 8, 4, 2, 73, 18, 3, 5, 4, 3, 4, 4, 1…
$ PWGTP13     <dbl> 21, 21, 4, 8, 26, 6, 4, 4, 2, 47, 29, 17, 31, 19, 23, 23, …
$ PWGTP14     <dbl> 13, 14, 4, 10, 28, 8, 4, 3, 3, 13, 5, 7, 15, 10, 10, 14, 1…
$ PWGTP15     <dbl> 12, 14, 14, 28, 8, 2, 15, 10, 8, 73, 18, 14, 27, 19, 20, 2…
$ PWGTP16     <dbl> 11, 11, 18, 42, 26, 8, 17, 20, 10, 39, 5, 9, 19, 13, 18, 1…
$ PWGTP17     <dbl> 3, 4, 17, 30, 23, 7, 11, 9, 8, 95, 16, 8, 19, 13, 12, 14, …
$ PWGTP18     <dbl> 14, 17, 16, 25, 5, 2, 12, 9, 8, 59, 26, 16, 31, 18, 19, 19…
$ PWGTP19     <dbl> 5, 4, 23, 33, 17, 8, 17, 9, 7, 13, 14, 8, 19, 9, 10, 16, 1…
$ PWGTP20     <dbl> 20, 20, 18, 40, 14, 7, 27, 25, 16, 65, 4, 6, 17, 10, 10, 1…
$ PWGTP21     <dbl> 15, 14, 33, 42, 16, 8, 4, 5, 3, 48, 19, 2, 6, 3, 3, 5, 5, …
$ PWGTP22     <dbl> 11, 11, 20, 40, 14, 8, 3, 3, 2, 15, 4, 15, 37, 26, 26, 28,…
$ PWGTP23     <dbl> 4, 4, 5, 6, 30, 9, 4, 4, 2, 58, 15, 13, 30, 16, 23, 23, 23…
$ PWGTP24     <dbl> 13, 12, 22, 46, 6, 2, 19, 15, 9, 41, 22, 9, 17, 13, 15, 19…
$ PWGTP25     <dbl> 16, 16, 17, 29, 9, 1, 40, 21, 13, 13, 26, 10, 19, 16, 12, …
$ PWGTP26     <dbl> 4, 4, 21, 43, 7, 2, 20, 12, 9, 13, 12, 7, 19, 9, 10, 17, 1…
$ PWGTP27     <dbl> 4, 5, 13, 26, 7, 2, 5, 6, 2, 47, 13, 2, 5, 4, 3, 5, 4, 17,…
$ PWGTP28     <dbl> 26, 27, 4, 9, 36, 11, 18, 17, 9, 39, 17, 7, 16, 9, 11, 15,…
$ PWGTP29     <dbl> 13, 13, 15, 29, 21, 6, 21, 13, 7, 49, 19, 14, 35, 15, 26, …
$ PWGTP30     <dbl> 12, 12, 4, 10, 25, 6, 5, 3, 2, 40, 20, 8, 23, 12, 12, 15, …
$ PWGTP31     <dbl> 21, 24, 27, 45, 6, 4, 13, 12, 15, 48, 4, 13, 31, 19, 24, 2…
$ PWGTP32     <dbl> 14, 14, 17, 29, 25, 16, 31, 22, 20, 63, 15, 16, 36, 15, 30…
$ PWGTP33     <dbl> 23, 24, 4, 8, 13, 6, 33, 25, 15, 57, 27, 3, 6, 2, 4, 5, 6,…
$ PWGTP34     <dbl> 11, 11, 4, 10, 20, 5, 36, 20, 11, 19, 5, 9, 21, 14, 14, 15…
$ PWGTP35     <dbl> 13, 13, 20, 32, 29, 10, 12, 12, 7, 80, 16, 2, 6, 4, 3, 4, …
$ PWGTP36     <dbl> 10, 11, 16, 31, 19, 8, 10, 10, 9, 52, 4, 10, 20, 11, 13, 1…
$ PWGTP37     <dbl> 4, 4, 17, 26, 20, 8, 15, 15, 9, 55, 15, 7, 18, 11, 18, 17,…
$ PWGTP38     <dbl> 17, 19, 9, 23, 26, 10, 19, 16, 9, 42, 26, 2, 5, 3, 4, 3, 3…
$ PWGTP39     <dbl> 4, 3, 13, 30, 17, 8, 18, 9, 8, 14, 11, 7, 19, 11, 14, 15, …
$ PWGTP40     <dbl> 26, 22, 9, 9, 14, 8, 35, 27, 19, 19, 24, 10, 17, 11, 15, 1…
$ PWGTP41     <dbl> 10, 12, 5, 7, 25, 5, 5, 5, 2, 43, 5, 12, 29, 21, 21, 22, 2…
$ PWGTP42     <dbl> 15, 13, 17, 32, 16, 6, 5, 2, 2, 65, 27, 2, 8, 3, 5, 4, 5, …
$ PWGTP43     <dbl> 5, 3, 23, 36, 37, 14, 4, 4, 3, 15, 14, 2, 5, 3, 3, 4, 5, 3…
$ PWGTP44     <dbl> 14, 12, 4, 8, 6, 3, 17, 11, 9, 34, 4, 6, 16, 10, 11, 13, 1…
$ PWGTP45     <dbl> 27, 24, 12, 29, 6, 3, 25, 21, 22, 55, 4, 7, 17, 8, 12, 11,…
$ PWGTP46     <dbl> 3, 4, 4, 13, 5, 2, 13, 11, 9, 71, 13, 9, 20, 13, 17, 20, 1…
$ PWGTP47     <dbl> 4, 3, 18, 41, 6, 2, 4, 5, 2, 46, 13, 16, 32, 18, 20, 29, 2…
$ PWGTP48     <dbl> 15, 18, 29, 58, 23, 18, 15, 13, 13, 45, 13, 9, 22, 10, 12,…
$ PWGTP49     <dbl> 12, 12, 15, 39, 22, 7, 14, 10, 9, 47, 15, 3, 7, 3, 4, 5, 5…
$ PWGTP50     <dbl> 13, 14, 25, 37, 13, 9, 4, 4, 3, 48, 14, 7, 21, 12, 13, 15,…
$ PWGTP51     <dbl> 18, 19, 4, 7, 11, 2, 17, 12, 9, 45, 22, 2, 6, 3, 4, 4, 4, …
$ PWGTP52     <dbl> 15, 14, 12, 25, 27, 13, 20, 17, 19, 14, 19, 2, 6, 4, 4, 4,…
$ PWGTP53     <dbl> 25, 23, 22, 37, 21, 7, 27, 20, 13, 48, 5, 10, 25, 17, 15, …
$ PWGTP54     <dbl> 15, 13, 29, 45, 25, 9, 29, 22, 17, 56, 21, 7, 17, 9, 17, 1…
$ PWGTP55     <dbl> 14, 15, 18, 25, 35, 9, 10, 11, 6, 17, 14, 16, 40, 22, 26, …
$ PWGTP56     <dbl> 13, 17, 13, 28, 20, 7, 15, 9, 8, 46, 17, 7, 19, 10, 13, 16…
$ PWGTP57     <dbl> 4, 4, 15, 22, 15, 6, 15, 10, 8, 13, 16, 7, 21, 11, 11, 13,…
$ PWGTP58     <dbl> 10, 11, 12, 25, 23, 8, 14, 11, 5, 45, 5, 13, 36, 18, 22, 2…
$ PWGTP59     <dbl> 4, 4, 14, 32, 22, 6, 17, 22, 9, 58, 16, 9, 23, 10, 12, 14,…
$ PWGTP60     <dbl> 20, 20, 4, 7, 27, 6, 4, 4, 2, 21, 22, 8, 21, 12, 12, 15, 1…
$ PWGTP61     <dbl> 12, 12, 5, 9, 25, 6, 24, 20, 13, 62, 5, 2, 5, 3, 4, 4, 4, …
$ PWGTP62     <dbl> 14, 14, 10, 24, 22, 7, 32, 21, 14, 79, 28, 15, 24, 17, 23,…
$ PWGTP63     <dbl> 4, 3, 27, 44, 7, 2, 28, 17, 9, 13, 15, 20, 39, 20, 22, 28,…
$ PWGTP64     <dbl> 12, 14, 6, 8, 44, 12, 15, 10, 8, 46, 7, 10, 20, 14, 11, 13…
$ PWGTP65     <dbl> 22, 23, 10, 25, 38, 12, 5, 4, 2, 87, 6, 8, 15, 11, 16, 14,…
$ PWGTP66     <dbl> 4, 3, 8, 10, 41, 11, 21, 16, 8, 81, 19, 8, 22, 9, 18, 15, …
$ PWGTP67     <dbl> 3, 4, 23, 34, 40, 12, 26, 17, 15, 46, 11, 3, 8, 5, 4, 5, 5…
$ PWGTP68     <dbl> 17, 18, 27, 49, 7, 2, 22, 14, 8, 48, 17, 11, 18, 11, 17, 1…
$ PWGTP69     <dbl> 14, 12, 13, 36, 18, 6, 13, 17, 7, 41, 16, 16, 35, 26, 24, …
$ PWGTP70     <dbl> 15, 13, 17, 38, 16, 6, 26, 24, 14, 52, 21, 8, 22, 14, 11, …
$ PWGTP71     <dbl> 21, 20, 5, 7, 25, 14, 17, 11, 11, 46, 31, 16, 38, 19, 16, …
$ PWGTP72     <dbl> 13, 17, 13, 19, 7, 3, 5, 4, 3, 12, 21, 13, 29, 17, 20, 24,…
$ PWGTP73     <dbl> 25, 24, 24, 46, 13, 7, 5, 4, 2, 49, 5, 3, 6, 4, 5, 4, 5, 2…
$ PWGTP74     <dbl> 13, 15, 31, 49, 15, 7, 6, 6, 2, 86, 24, 11, 16, 9, 11, 15,…
$ PWGTP75     <dbl> 11, 13, 10, 28, 8, 2, 10, 13, 7, 21, 18, 3, 6, 3, 4, 5, 5,…
$ PWGTP76     <dbl> 15, 13, 14, 37, 17, 5, 17, 11, 5, 56, 25, 12, 21, 11, 14, …
$ PWGTP77     <dbl> 5, 5, 11, 21, 25, 8, 22, 17, 9, 14, 15, 10, 20, 10, 9, 12,…
$ PWGTP78     <dbl> 12, 12, 14, 27, 6, 3, 14, 10, 11, 51, 5, 2, 5, 3, 3, 4, 4,…
$ PWGTP79     <dbl> 3, 4, 20, 31, 20, 11, 16, 11, 12, 81, 16, 11, 19, 13, 18, …
$ PWGTP80     <dbl> 21, 23, 17, 39, 16, 6, 5, 3, 2, 91, 5, 10, 20, 12, 13, 17,…

Make custom estimates

  • Example: Level of school attendance among those who have finished high school, age 18-25
library(srvyr)

pums_recoded %>%
  to_survey() %>%
  filter(AGEP %in% c(18:25)) %>%
  filter(as.numeric(SCHL) >= 16) %>%
  group_by(PUMA, SCHG_label) %>%
  summarize(
    N = survey_total(),
    Pct = survey_mean() * 100
  )

Make custom estimates

  • Example: Level of school attendance among those who have finished high school, age 18-25
# A tibble: 8 × 6
# Groups:   PUMA [2]
  PUMA  SCHG_label                                         N  N_se    Pct Pct_se
  <chr> <ord>                                          <dbl> <dbl>  <dbl>  <dbl>
1 01301 N/A (not attending school)                      9769 719.  42.1    2.46 
2 01301 Grade 12                                          66  55.9  0.284  0.239
3 01301 College undergraduate years (freshman to seni… 10365 464.  44.7    1.76 
4 01301 Graduate or professional school beyond a bach…  3013 352.  13.0    1.47 
5 01302 N/A (not attending school)                      7715 656.  55.5    3.28 
6 01302 Grade 12                                         429 218.   3.08   1.53 
7 01302 College undergraduate years (freshman to seni…  4533 484.  32.6    3.16 
8 01302 Graduate or professional school beyond a bach…  1234 235.   8.87   1.61 

Questions