tiger_tracts_sf {RcensusPkg}R Documentation

tiger_tracts_sf

Description

This function performs three tasks:

  1. Download to an output directory a zip file from the TIGER/Line Shapefiles database.

  2. Unzip the zip file and locate the shape file of interest.

  3. Read and convert the shape file to a simple feature object.

Usage

tiger_tracts_sf(
  state = NULL,
  output_dir = tempdir(),
  delete_files = TRUE,
  vintage = 2020,
  general = FALSE,
  set_crs = NULL,
  transform_crs = NULL,
  sf_info = FALSE,
  do_progress = FALSE,
  shapefile = NULL,
  datafile = NULL,
  datafile_key = NULL,
  sf_key = "GEOID",
  express = NULL,
  check_na = FALSE
)

Arguments

state

A required two-digit FIPS code for the state of interest. See usmap::fips function for finding FIPS codes.

output_dir

A full directory path where the shapefile and its associated files will be downloaded. The default is the directory defined by the value returned by tempdir().

delete_files

A logical which if TRUE will delete the shapefile and associated files in 'output_dir'. The default is TRUE.

vintage

A required numeric that sets the vintage of interest. The default is 2020.

general

A logical which if TRUE will download a less detailed, more generalized version of the tract geometries.

set_crs

A numeric or character string which if non-NULL calls sf::st_crs() to set the crs of the geometries and transforms them.

transform_crs

A numeric or character string which if non-NULL calls sf::st_transform() to perform a crs transform of the geometries. Note that the crs of the shapefile must not be NA.

sf_info

A logical which if TRUE displays info on the resulting simple feature object.

do_progress

A logical which if TRUE displays a progress bar during the download.

shapefile

A full file path to a shapefile folder with its unzipped files to be processed instead of downloading.

datafile

A dataframe containing data that should be joined with this function's resultant simple feature object.

datafile_key

The column name from 'datafile' dataframe used to key with the 'sf_key' column of the resultant simple feature dataframe.

sf_key

The column from the resultant dataframe used to key with the 'datafile' dataframe.

express

A logical expression object used to filter the resultant simple feature dataframe. For example, one of the columns of the resultant simple feature dataframe is "COUNTYFP". If you wanted to return just the geometries for Los Alamos, New Mexico (which has a fips code of "028"), then you assign 'express' equal to: expression(COUNTYFP == "028"). The expression will be evaluated and only the tract geometries for Los Alamos will be returned.

check_na

A logical which if TRUE will remove rows that have missing values for any of the columns. The default is to not check the columns for NA values.

Details

Returns simple feature (sf) of US Census tract boundary related geometric polygons, provided by the US Census Bureau's TIGER/Line Shapefiles database. See Simple Features for R for more information on simple features. Along with the geometries, additional tract related variables are provided. See Appendix G-3. Record Layout: Census Tract State-based Shapefile) for a description of tract related variables of the sf file. For further information on the Census Bureau's shape files see About the 2021 TIGER/Line Shapefiles. From Chapter 4.4 Census Tracts – "Census tracts are small and relatively permanent statistical subdivisions of a county or equivalent entity. Local participants review and update census tracts prior to each decennial census as part of the Census Bureau’s PSAP."

A more generalized, recognizable version of the tract geometries that has less download size is also available. For more information on cartographic boundary files see Cartographic Boundary File Description. These files are available for vintages greater than 2013 with resolution 1:500k meters.

The function returns the simple feature object which can easily be mapped (see RplotterPkg::create_sf_plot()) or joined with US Census Bureau demographic data. To help incorporate data files, this function has a datafile parameter which will be joined with the resultant simple feature object. The only requirement is that a common "key" for joining exist between the data dataframe and the simple feature dataframe.

Some earlier vintages may have NA for the crs so you may need to specify the 'crs_transform' to 3426. Also you may be interested in using a state level crs. See epsg.io to search worldwide for crs.

Value

A data frame object of class sf (simple feature)

Examples

library(downloader)
library(sf)
library(data.table)
library(usmap)
library(withr)
library(RcensusPkg)

# Get the generalized tract geometries for Los Alamos, New Mexico
# Determine the fips codes for New Mexico and Los Alamos
nm_los_alamos_fips <- usmap::fips(state = "new mexico", county = "los alamos")
nm_fips <- substr(nm_los_alamos_fips, 1, 2)
los_alamos_fips <- substr(nm_los_alamos_fips, 3, 5)

# Create an expression to filter just the Los Alamos geometries
express <- parse(text = paste0("COUNTYFP == ", '"', los_alamos_fips, '"'))

# Define a temporary output folder for the downloaded shapefiles
output_dir <- withr::local_tempdir()
if(!dir.exists(output_dir)){
  dir.create(output_dir)
}

# Get the tract geometries for just Los Alamos
losalamos_tracts_sf <- RcensusPkg::tiger_tracts_sf(
  state = nm_fips,
  general = TRUE,
  express = express,
  output_dir = output_dir,
  delete_files = FALSE
)


[Package RcensusPkg version 0.1.5 Index]