Reading 2020 ZCTA Codes for North Carolina using R
In this article, we will explore how to download and read 2020 ZCTA (Zip Code Tabulation Area) codes for North Carolina using R programming language. ZCTAs are generalized areal representations of United States Postal Service (USPS) ZIP codes and are used for statistical purposes by the United States Census Bureau.
Downloading ZCTA Shapefiles for North Carolina
To download the ZCTA shapefiles for North Carolina, we will use the tigris package in R. The tigris package provides access to the US Census Bureau's Topologically Integrated Geographic Encoding and Referencing (TIGER) data, including Census boundaries and geography.
To download the ZCTA shapefiles for North Carolina, we can use the zctas() function in the following way:
library(tidyverse)
library(tigris)
library(sf)
nc\_zips\_source <- zctas(cb = TRUE, state = "NC", year = 2020)In the above code:
cb = TRUEindicates that we want to download the Census Bureau's Cartographic Boundary Shapefiles.state = "NC"specifies the state we are interested in, which is North Carolina in this case.year = 2020specifies the year of the ZCTAs we want to download.
After running the above code, the ZCTA shapefiles for North Carolina will be downloaded and stored in the nc_zips_source variable as a sf object.
Reading ZCTA Codes from the Shapefiles
To extract the ZCTA codes from the shapefiles, we can use the st\_drop_geometry() function from the sf package to remove the geometry column and convert the shapefiles into a regular data frame. We can then select the ZCTA code column using the $ operator.
nc\_zips <- st\_drop\_geometry(nc\_zips\_source)The nc_zips variable now contains a data frame with the ZCTA codes and other attributes for the ZCTAs in North Carolina.
In this article, we covered how to download and read 2020 ZCTA codes for North Carolina using R programming language. We used the tigris package to download the ZCTA shapefiles for North Carolina and the sf package to extract the ZCTA codes into a regular data frame.
References
- tigris: Access TIGER/Line Shapefiles from R
- st\_drop\_geometry - Simple Features for R
- U.S. Census Bureau, TIGER/Line Shapefiles, 2020
This article was generated using the following R code:
---
title: "Read 2020 ZCTA Codes for North Carolina using R"
description: |
<p>This article covers how to download and read 2020 ZCTA codes for North Carolina using R programming language. We will use the tigris package to download the ZCTA shapefiles for North Carolina and the sf package to extract the ZCTA codes into a regular data frame.</p>
output:
html_document:
toc: true
code\_folding: show
fig\_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\<h2>Reading 2020 ZCTA Codes for North Carolina using R\</h2>
\<p>In this article, we will explore how to download and read 2020 ZCTA (Zip Code Tabulation Area) codes for North Carolina using R programming language. ZCTAs are generalized areal representations of United States Postal Service (USPS) ZIP codes and are used for statistical purposes by the United States Census Bureau.</p>
\<h2>Downloading ZCTA Shapefiles for North Carolina</h2>
\<p>To download the ZCTA shapefiles for North Carolina, we will use the <code>tigris</code> package in R. The <code>tigris</code> package provides access to the US Census Bureau's Topologically Integrated Geographic Encoding and Referencing (TIGER) data, including Census boundaries and geography.</p>
\<p>To download the ZCTA shapefiles for North Carolina, we can use the <code>zctas()</code> function in the following way:</p>
\< code>
library(tidyverse)
library(tigris)
library(sf)
nc\_zips\_source <- zctas(cb = TRUE, state = "NC", year = 2020)
\</code>
\<p>In the above code:</p>
\<ul>
\<li><code>cb = TRUE</code> indicates that we want to download the Census Bureau's Cartographic Boundary Shapefiles.</li>
\<li><code>state = "NC"</code> specifies the state we are interested in, which is North Carolina in this case.</li>
\<li><code>year = 2020</code> specifies the year of the ZCTAs we want to download.</li>
\</ul>
\<p>After running the above code, the ZCTA shapefiles for North Carolina will be downloaded and stored in the <code>nc\_zips\_source</code> variable as a <code>sf</code> object.</p>
\<h2>Reading ZCTA Codes from the Shapefiles</h2>
\<p>To extract the ZCTA codes from the shapefiles, we can use the <code>st\_drop\_geometry()</code> function from the <code>sf</code> package to remove the geometry column and convert the shapefiles into a regular data frame. We can then select the ZCTA code column using the <code>$</code> operator.</p>
\<code>
nc\_zips <- st\_drop\_geometry(nc\_zips\_source)
\</code>
\<p>The <code>nc\_zips</code> variable now contains a data frame with the ZCTA codes and other attributes for the ZCTAs in North Carolina.</p>
\<h3>Summary</h3>
\<p>In this article, we covered how to download and read 2020 ZCTA codes for North Carolina using R programming language. We used the <code>tigris</code> package to download the ZCTA shapefiles for North Carolina and the <code>sf</code> package to extract the ZCTA codes into a regular data frame.</p>
\<h3>References</h3>
\<ul>
\<li<a href="">tigris: Access TIGER/Line Shapefiles from R</a></li>
\<li<a href="">st\_drop\_geometry - Simple Features for R</a></li>
\<li<a href="