-
-
Save cbgoodman/8b78153e93148cdff370e3c6f3629ade to your computer and use it in GitHub Desktop.
R code to import all years of county-level building permit data and lightly clean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("tidyverse") | |
library("rvest") | |
## Import ---- | |
### The directory location | |
url_loc <- "www2.census.gov/econ/bps/County/" | |
files <- rvest::read_html("https://www2.census.gov/econ/bps/County/") |> | |
rvest::html_elements("a") |> | |
rvest::html_text2() | |
### Just take files containing `coYYYYa`` | |
fname_stubs <- files[str_detect(files, "(co)\\d{4}(:?a)")] | |
# remove 2022 update file | |
fname_stubs <- fname_stubs[-34] | |
### Add year | |
fname_year <- substr(fname_stubs, start = 3, stop = 6) | |
### Clean up the URLs | |
fnames <- paste0("https://", url_loc, fname_stubs) | |
fnames <- set_names(fnames, fname_year) | |
### Import | |
cpb <- fnames |> | |
map(~ read_csv(., skip = 1)) |> | |
list_rbind(names_to = "year") |> | |
rename( | |
survey = Date, | |
state_fips = State, | |
county_fips = County, | |
region = "Code...4", | |
division = "Code...5", | |
county_name = Name, | |
bldg_1unit = "Bldgs...7", | |
units_1unit = "Units...8", | |
val_1unit = "Value...9", | |
bldg_2unit = "Bldgs...10", | |
units_2unit = "Units...11", | |
val_2unit = "Value...12", | |
bldg_34unit = "Bldgs...13", | |
units_34unit = "Units...14", | |
val_34unit = "Value...15", | |
bldg_5unit = "Bldgs...16", | |
units_5unit = "Units...17", | |
val_5unit = "Value...18", | |
bldg_1unit_rep = "Bldgs...19", | |
units_1unit_rep = "Units...20", | |
val_1unit_rep = "Value...21", | |
bldg_2unit_rep = "Bldgs...22", | |
units_2unit_rep = "Units...23", | |
val_2unit_rep = "Value...24", | |
bldg_34unit_rep = "Bldgs...25", | |
units_34unit_rep = "Units...26", | |
val_34unit_rep = "Value...27", | |
bldg_5unit_rep = "Bldgs...28", | |
units_5unit_rep = "Units...29", | |
val_5unit_rep = "Value...30" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment