knitr::opts_chunk$set(echo = TRUE)
In this vignette, we show how we prepared the crosswalk dataset that is attached to the hyspdisp
package. In case you want to use a different crosswalk you can follow some of these preprocessing steps.
First, we load the packages that we will need for completing this task.
library(disperseR) library(data.table) library(tidyverse)
In case you have not yet created your project folder use the following command.
disperseR::create_dirs()
If the file does not exist, download it from USDMAPPER. Then, open it.
filename<- file.path(main_dir, "crosswalk.xlsx") if(!file.exists(filename)){ download.file("http://www.udsmapper.org/docs/zip_to_zcta_2017.xlsx", filename) } crosswalk <- readxl::read_excel(filename, skip = 0)
Next step is to rename the ZIP_CODE
, CITY
variables.
crosswalk <- crosswalk %>% dplyr::rename(.,ZIP=ZIP_CODE, CITY = PO_NAME)
We use the 2017 census data packaged by American Fact Finder.
We put the downloaded csv file in our main directory that has been specified in the createdirs()
function and read it. We are also cleaning a the column names a little bit.
file <- file.path(main_dir, "ACS_17_5YR_B01003_with_ann.csv") ZCTApop2017 <- read.csv(file, skip = 1, header = T) names(ZCTApop2017) <- c('ID', 'ID2', 'GEOGRAPHY', 'TOTALESTIMATE', 'MARGINOFERROR')
Now, we create a ZCTA code variable by extracting the 5 last digits from the id
variable.
ZCTApop2017 <- ZCTApop2017 %>% dplyr::mutate(ZCTA=stringr::str_sub(ID,-5,-1))
Merge the data to the crosswalk.
crosswalk <- dplyr::inner_join(crosswalk, ZCTApop2017)
Keep only some of the variables.
crosswalk<- crosswalk %>% dplyr::select(ZIP, CITY, ZCTA, STATE, TOTALESTIMATE, MARGINOFERROR)
Transform to data.table.
crosswalk <- data.table::data.table(crosswalk)
Now the crosswalk is ready to use.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.