Back to Programming Texas County Names and FIPS Codes
library(magrittr)
library(xml2)
library(rvest)
# Scrape HTML table with county names and FIPS codes from Texas Department of Safety and Health Services website
countycodestx <- "https://www.dshs.texas.gov/chs/info/info_txco.shtm" %>% 
  read_html() %>%
  html_nodes(xpath = '//*[contains(concat( " ", @class, " " ), concat( " ", "table", " " ))]') %>%
  html_table(header = TRUE, trim = TRUE) %>%
  as.data.frame()
# Create countycodesdfw as subset of countycodestx located in listed DFW counties
dfwcounties <- c("Collin", "Dallas", "Denton", "Ellis", "Hunt", "Kaufman", 
                 "Rockwall", "Johnson", "Parker", "Tarrant", "Wise")
countycodesdfw <- countycodestx[countycodestx$CountyName %in% dfwcounties,]

Texas has 254 counties, of which 11 are located in the Dallas-Fort Worth-Arlington metropolitan statistical area (MSA).

library(stringr)
# Extract state FIPS and county FIPS from FIPSCode column
countycodesdfw$StateFIPS <- str_extract(countycodesdfw$FIPSCode, "^[[:digit:]]{2}")
countycodesdfw$CountyFIPS <- str_extract(countycodesdfw$FIPSCode, "[[:digit:]]{3}$")
# Select columns to keep in countycodesdfw table below
countycodesdfw <- subset(countycodesdfw, select = c(CountyName, StateFIPS, CountyFIPS))
Counties in DFW
County Name State FIPS County FIPS
Collin 48 085
Dallas 48 113
Denton 48 121
Ellis 48 139
Hunt 48 231
Johnson 48 251
Kaufman 48 257
Parker 48 367
Rockwall 48 397
Tarrant 48 439
Wise 48 497