Skip to content

Instantly share code, notes, and snippets.

@dawaldron
Created January 17, 2024 02:31
Show Gist options
  • Save dawaldron/e5db1056d84a61f56d52407c9dc02934 to your computer and use it in GitHub Desktop.
Save dawaldron/e5db1056d84a61f56d52407c9dc02934 to your computer and use it in GitHub Desktop.
Reads seasonally-adjusted LAUS data for MSAs
require(data.table)
require(magrittr)
# bls.gov now requires contact information to download its text files
email <- ''
dt_msa <- readLines(url('https://www.bls.gov/web/metro/ssamatab1.txt',
headers = c('User-Agent' = email))) %>%
.[-(1:5)] %>%
data.table(var = .) %>%
.[,
.(StateFIPS = substr(var, 21, 22),
AreaFIPS = substr(var, 30, 34),
AreaName = trimws(substr(var, 41, 97)),
Year = as.integer(substr(var, 100, 103)),
Month = as.integer(substr(var, 108, 109)),
Date = as.Date(paste0(substr(var, 100, 103), '-', substr(var, 108, 109), '-01')),
LaborForce = as.integer(gsub(',', '', substr(var, 110, 123), fixed = TRUE)),
Employment = as.integer(gsub(',', '', substr(var, 124, 141), fixed = TRUE)),
Unemployment = as.integer(gsub(',', '', substr(var, 142, 155), fixed = TRUE)),
UnempRate = as.numeric(substr(var, 156, 169)))] %>%
.[!is.na(Year)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment