This function takes a CBG code (as numeric or string) and returns the state and county FIPS codes associated with it.

fips_from_cbg(cbg, return = "both")

## Arguments

cbg

CBG code, in numeric or string form. To aid speed since this function is called millions of times, cbg is not checked to ensure it is a valid CBG identifier.

return

Set to 'state' to get back only state FIPS, 'county' for only county, or 'both' for a list of both (state then county).

## Details

The syntax for this function was developed before the Canadian data was introduced, so it is definitely US-first, down to the function name, with Canadian additions tacked on. Sorry neighbors to the North. Canadian province ("state") and census division ("county") identifiers will be preceded with "CA:" as in the SafeGraph cbg variable.

This function now returns character values rather than numeric, to account for the Canadian data.

Why does this produce a list and not a vector? For data.table usage.

## Examples


a_cbg <- '560610112022'
fips_from_cbg(a_cbg)
#> [[1]]
#> [1] "56"
#>
#> [[2]]
#> [1] "061"
#>

# Use with data.table!
DT <- data.table::data.table(cbg = c('560610112022','10310112022'))
DT[,c('state_fips','county_fips') := fips_from_cbg(cbg)]
#>             cbg state_fips county_fips
#> 1: 560610112022         56         061
#> 2:  10310112022         01         031