rshapefilezipcodetigris

How can I read in the 2020 ZCTA codes for North Carolina using the zctas() function?


I am trying to download the zip code boundaries for North Carolina using the following code in R Studio...

library(tidyverse)
library(tigris)
library(sf)

nc_zips_source <- zctas(cb = TRUE,
                       starts_with = "27", "28",
                       year = 2020)

When I run the code above, I get the following error in my console...

Error in zctas(cb = TRUE, starts_with = "27", "28", year = 2020) : 
  ZCTAs are only available by state for 2000 and 2010.

According to the Census website, not all valid zip codes are represented by the 2020 ZCTAs. Perhaps North Carolina zip codes are not available for 2020? Does anyone have any suggestions on how I can access the 2020 Census tabulation block boundaries for North Carolina without downloading the shapefile and processing it from scratch?


Solution

  • zcta() interprets the "28" argument in your call as an argument for state. Hence the error message points out, that the ZCTA data by state is only available for 2000 and 2010.

    Change the starts_with = "27", "28", part to starts_with = 27:28 or starts_with = c(27, 28) to fix the issue.

    library(tidyverse)
    library(tigris)
    
    zcta_nc <- zctas(cb = TRUE,
                     starts_with = 27:28,
                     year = 2020)