Title: | Making Census Data More Usable |
---|---|
Description: | Creates a common framework for organizing, naming, and gathering population, age, race, and ethnicity data from the Census Bureau. Accesses the API <https://www.census.gov/data/developers/data-sets.html>. Provides tools for adding information to existing data to line up with Census data. |
Authors: | Christopher T. Kenny [aut, cre]
|
Maintainer: | Christopher T. Kenny <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.7 |
Built: | 2024-08-28 02:37:23 UTC |
Source: | https://github.com/christopherkenny/censable |
Adds a value to the Renvironment of the form name=value
. Designed for flexibly
adding API keys for future sessions. Defaults are set up for entering a Census API
key to work with tidycensus
. By default this key will be configured to work
with tidycensus
. Package internally allows this key to work with censusapi
when used through censable
.
add_r_environ( value, name = "CENSUS_API_KEY", overwrite = FALSE, install = FALSE )
add_r_environ( value, name = "CENSUS_API_KEY", overwrite = FALSE, install = FALSE )
value |
Character. Value to add. |
name |
Defaults to CENSUS_API_KEY. Character. Name to give |
overwrite |
Defaults to FALSE. Boolean. Should existing item with name |
install |
Defaults to FALSE. Boolean. Should this be added '~/.Renviron' file? |
value, invisibly
## Not run: add_r_environ('1234', 'SECRET_API_KEY') ## End(Not run)
## Not run: add_r_environ('1234', 'SECRET_API_KEY') ## End(Not run)
Breakdown Census GEOID into Components
breakdown_geoid(.data, GEOID = "GEOID", area_type = "spine")
breakdown_geoid(.data, GEOID = "GEOID", area_type = "spine")
.data |
dataframe, tibble, or sf tibble |
GEOID |
Column in .data with Census GEOID |
area_type |
String, default is 'spine' with type of GEOID. Options are 'spine' for states, counties, tracts, block groups, and blocks. 'shd' for lower state legislative districts, 'ssd' for upper state legislative districts, 'cd' for congressional districts, or 'zcta' for zip code tabulation areas. |
.data with added identifying columns based on area_type
data(mt_county) mt_county <- mt_county %>% breakdown_geoid()
data(mt_county) mt_county <- mt_county %>% breakdown_geoid()
Creates a dataset, using the decennial census information, with the
standard variables used for redistricting. Creates a stable base for getting
data from censusapi
for common calls in redistricting.
#' # Output columns are:
GEOID: Geographic Identifier
NAME: Name of County
pop: total population
pop_white: total population, Non-Hispanic White
pop_black: total population, Non-Hispanic Black
pop_hisp: total population, Hispanic
pop_aian: total population, Non-Hispanic American Indian and Alaskan Native
pop_asian: total population, Non-Hispanic Asian
pop_nhpi: total population, Non-Hispanic Native Hawaiian and Pacific Islander
pop_other: total population, Non-Hispanic Other
pop_two: total population, Non-Hispanic Two Plus Races
vap: voting age population
vap_white: voting age population, Non-Hispanic White
vap_black: voting age population, Non-Hispanic Black
vap_hisp: voting age population, Hispanic
vap_aian: voting age population, Non-Hispanic American Indian and Alaskan Native
vap_asian: voting age population, Non-Hispanic Asian
vap_nhpi: voting age population, Non-Hispanic Native Hawaiian and Pacific Islander
vap_other: voting age population, Non-Hispanic Other
vap_two: voting age population, Non-Hispanic Two Plus Races
geometry: sf geometry
Arguments for geography
are not checked, so will error if invalid.
This is by design to avoid blocking usage that could become valid.
Currently valid options for geography
:
'state'
'county'
'tract'
'block group'
'block'
'county subdivision'
'zcta'
'congressional district'
'state legislative district (upper chamber)'
'state legislative district (lower chamber)'
'school district (unified)'
'school district (elementary)'
'school district (secondary)'
build_acs( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, survey = "acs5", groups = "all" ) mem_build_acs( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, survey = "acs5", groups = "all" )
build_acs( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, survey = "acs5", groups = "all" ) mem_build_acs( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, survey = "acs5", groups = "all" )
geography |
Required. The geography level to use. |
state |
Required. Two letter state postal code. |
county |
Optional. Name of county. If not provided, returns blocks for the entire state. |
geometry |
Defaults to TRUE. Whether to return the geometry or not. |
year |
year, must be 2000, 2010, or 2020 (after August 2021) |
survey |
whether the get estimates from the 5-year ('acs5'), 3-year ('acs3'), or 1-year ('acs1') survey. Default is 'acs5'. |
groups |
defaults to 'all', which gets pop and vap. If 'pop', only gets pop. If 'vap', only gets vap. Any other strings default to 'all'. |
tibble with observations for each observation of the geography in the state or county. Data includes up to 3 sets of columns for each race or ethnicity category: population (pop), voting age population (vap), and citizen voting age population (cvap)
## Not run: # uses the Census API tb <- build_acs(geography = 'tract', state = 'NY', county = 'Rockland', geometry = TRUE) ## End(Not run)
## Not run: # uses the Census API tb <- build_acs(geography = 'tract', state = 'NY', county = 'Rockland', geometry = TRUE) ## End(Not run)
Creates a dataset, using the decennial census information, with the
standard variables used for redistricting. Creates a stable base for getting
data from censusapi
for common calls in redistricting.
build_dec( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, groups = "all" ) mem_build_dec( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, groups = "all" )
build_dec( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, groups = "all" ) mem_build_dec( geography, state = NULL, county = NULL, geometry = TRUE, year = 2020, groups = "all" )
geography |
Required. The geography level to use. |
state |
Required. Two letter state postal code. |
county |
Optional. Name of county. If not provided, returns blocks for the entire state. |
geometry |
Defaults to TRUE. Whether to return the geometry or not. |
year |
year, must be 2000, 2010, or 2020 (after August 2021) |
groups |
defaults to |
tibble with observations for each observation of the geography in the state or county. Data includes up to 2 sets of columns for each race or ethnicity category: population (pop) and voting age population (vap)
GEOID: Geographic Identifier
NAME: Name of County
pop: total population
pop_white: total population, Non-Hispanic White
pop_black: total population, Non-Hispanic Black
pop_hisp: total population, Hispanic
pop_aian: total population, Non-Hispanic American Indian and Alaskan Native
pop_asian: total population, Non-Hispanic Asian
pop_nhpi: total population, Non-Hispanic Native Hawaiian and Pacific Islander
pop_other: total population, Non-Hispanic Other
pop_two: total population, Non-Hispanic Two Plus Races
vap: voting age population
vap_white: voting age population, Non-Hispanic White
vap_black: voting age population, Non-Hispanic Black
vap_hisp: voting age population, Hispanic
vap_aian: voting age population, Non-Hispanic American Indian and Alaskan Native
vap_asian: voting age population, Non-Hispanic Asian
vap_nhpi: voting age population, Non-Hispanic Native Hawaiian and Pacific Islander
vap_other: voting age population, Non-Hispanic Other
vap_two: voting age population, Non-Hispanic Two Plus Races
geometry: sf geometry
Arguments for geography
are not checked, so will error if invalid.
This is by design, to avoid blocking usage that could become valid.
Currently valid options for geography
:
'state'
'county'
'tract'
'block group'
'block'
'county subdivision'
'zcta'
'congressional district'
'state legislative district (upper chamber)'
'state legislative district (lower chamber)'
'school district (unified)'
'school district (elementary)'
'school district (secondary)'
'voting district' may also work, though seems to be less reliable
## Not run: # uses the Census API tb <- build_dec(geography = 'block', state = 'NY', county = 'Rockland', geometry = TRUE) ## End(Not run)
## Not run: # uses the Census API tb <- build_dec(geography = 'block', state = 'NY', county = 'Rockland', geometry = TRUE) ## End(Not run)
Collapses Other, AIAN, Asian, NHPI, and Two+ into other, by prefix.
collapse4(.data, prefix)
collapse4(.data, prefix)
.data |
tibble, data.frame, or sf tibble |
prefix |
The prefix(es) for the race categories. Must be a character vector. |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse4(prefix = c('pop_', 'vap_'))
data(mt_county) mt_county <- mt_county %>% collapse4(prefix = c('pop_', 'vap_'))
Collapses Other, AIAN, Asian, NHPI, and Two+ into other.
collapse4_pop(.data, prefix = "pop_")
collapse4_pop(.data, prefix = "pop_")
.data |
tibble, data.frame, or sf tibble |
prefix |
Default is |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse4_pop()
data(mt_county) mt_county <- mt_county %>% collapse4_pop()
Collapses Other, AIAN, Asian, NHPI, and Two+ into other.
collapse4_vap(.data, prefix = "vap_")
collapse4_vap(.data, prefix = "vap_")
.data |
tibble, data.frame, or sf tibble |
prefix |
Default is |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse4_vap()
data(mt_county) mt_county <- mt_county %>% collapse4_vap()
Collapses Other, AIAN, NHPI, and Two+ into Other, by prefix.
collapse5(.data, prefix)
collapse5(.data, prefix)
.data |
tibble, data.frame, or sf tibble |
prefix |
The prefix(es) for the race categories. Must be a character vector. |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse5(prefix = c('pop_', 'vap_'))
data(mt_county) mt_county <- mt_county %>% collapse5(prefix = c('pop_', 'vap_'))
Collapses Other, AIAN, NHPI, and Two+ into other.
collapse5_pop(.data, prefix = "pop_")
collapse5_pop(.data, prefix = "pop_")
.data |
tibble, data.frame, or sf tibble |
prefix |
Default is |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse5_pop()
data(mt_county) mt_county <- mt_county %>% collapse5_pop()
Collapses Other, AIAN, NHPI, and Two+ into other.
collapse5_vap(.data, prefix = "vap_")
collapse5_vap(.data, prefix = "vap_")
.data |
tibble, data.frame, or sf tibble |
prefix |
Default is |
.data with columns collapsed
data(mt_county) mt_county <- mt_county %>% collapse5_vap()
data(mt_county) mt_county <- mt_county %>% collapse5_vap()
Create GEOID from Default Columns
construct_geoid( .data, area_type, state = "state", county = "county", tract = "tract", block_group = "block group", block = "block", cd = "cd", shd = "shd", ssd = "ssd", zcta = "zcta" )
construct_geoid( .data, area_type, state = "state", county = "county", tract = "tract", block_group = "block group", block = "block", cd = "cd", shd = "shd", ssd = "ssd", zcta = "zcta" )
.data |
dataframe, tibble, or sf tibble |
area_type |
Defaults to creating the smallest possible with 'spine' for states, counties, tracts, block groups, and blocks. You can also pass one of the on spine geographies to create that specific level. Other options are 'shd' for lower state legislative districts, 'ssd' for upper state legislative districts, 'cd' for congressional districts, or 'zcta' for zip code tabulation areas. |
state |
name of column with state component |
county |
name of column with county component |
tract |
name of column with tract component |
block_group |
name of column with block group component |
block |
name of column with block component |
cd |
name of column with cd component |
shd |
name of column with shd component |
ssd |
name of column with ssd component |
zcta |
name of column with zcta component |
.data with new column GEOID
data(mt_county) mt_county <- mt_county %>% breakdown_geoid() mt_county <- mt_county %>% dplyr::select(-dplyr::all_of('GEOID')) mt_county <- mt_county %>% construct_geoid()
data(mt_county) mt_county <- mt_county %>% breakdown_geoid() mt_county <- mt_county %>% dplyr::select(-dplyr::all_of('GEOID')) mt_county <- mt_county %>% construct_geoid()
Create a GEOID from Columns
custom_geoid(.data, ...)
custom_geoid(.data, ...)
.data |
dataframe, tibble, or sf tibble |
... |
columns of .data in the order you want to make the GEOID |
.data with new column GEOID
data(mt_county) mt_county <- mt_county %>% custom_geoid(GEOID)
data(mt_county) mt_county <- mt_county %>% custom_geoid(GEOID)
Contains three columns:
state: state FIPS
county: county FIPS
name: county name
data('fips_2000')
data('fips_2000')
tibble
data('fips_2000')
data('fips_2000')
Contains three columns:
state: state FIPS
county: county FIPS
name: county name
data('fips_2010')
data('fips_2010')
tibble
data('fips_2010')
data('fips_2010')
Contains three columns:
state: state FIPS
county: county FIPS
name: county name
data('fips_2020')
data('fips_2020')
tibble
data('fips_2020')
data('fips_2020')
Adds a column with state abbreviation joining by a column with state ansi
join_abb_ansi(.data, .ansi)
join_abb_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state abbreviation
data('stata') stata %>% join_abb_ansi(ansi)
data('stata') stata %>% join_abb_ansi(ansi)
Adds a column with state abbreviation joining by a column with state fips
join_abb_fips(.data, .fips)
join_abb_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state abb
data('stata') stata %>% join_abb_fips(fips)
data('stata') stata %>% join_abb_fips(fips)
Adds a column with state abbs joining by a column with state names
join_abb_name(.data, .name)
join_abb_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with abbreviation
data('stata') stata %>% join_abb_name(name)
data('stata') stata %>% join_abb_name(name)
Adds a column with state ansi joining by a column with state abbreviation
join_ansi_abb(.data, .abb)
join_ansi_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbreviation |
.data with column .abb replaced with state ansi
data('stata') stata %>% join_ansi_abb(abb)
data('stata') stata %>% join_ansi_abb(abb)
Adds a column with state ansi joining by a column with state fips
join_ansi_fips(.data, .fips)
join_ansi_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state ansi
data('stata') stata %>% join_ansi_fips(fips)
data('stata') stata %>% join_ansi_fips(fips)
Adds a column with state ansi joining by a column with state name
join_ansi_name(.data, .name)
join_ansi_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with ansi
data('stata') stata %>% join_ansi_name(name)
data('stata') stata %>% join_ansi_name(name)
Adds a column with state fips joining by a column with state abbreviation
join_fips_abb(.data, .abb)
join_fips_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbreviation |
.data with column .abb replaced with state name
data('stata') stata %>% join_fips_abb(abb)
data('stata') stata %>% join_fips_abb(abb)
Adds a column with state fips joining by a column with state ansi
join_fips_ansi(.data, .ansi)
join_fips_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state fips
data('stata') stata %>% join_fips_ansi(ansi)
data('stata') stata %>% join_fips_ansi(ansi)
Adds a column with state fips joining by a column with state name
join_fips_name(.data, .name)
join_fips_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with fips
data('stata') stata %>% join_fips_name(name)
data('stata') stata %>% join_fips_name(name)
Adds a column with state name joining by a column with state abbreviation
join_name_abb(.data, .abb)
join_name_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbreviation |
.data with column .abb replaced with state name
data('stata') stata %>% join_name_abb(abb)
data('stata') stata %>% join_name_abb(abb)
Adds a column with state name joining by a column with state ansi
join_name_ansi(.data, .ansi)
join_name_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state name
data('stata') stata %>% join_name_ansi(name)
data('stata') stata %>% join_name_ansi(name)
Adds a column with state name joining by a column with state fips
join_name_fips(.data, .fips)
join_name_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state name
data('stata') stata %>% join_name_fips(fips)
data('stata') stata %>% join_name_fips(fips)
Check or Get Census API Key
has_census_key() get_census_key(key = "")
has_census_key() get_census_key(key = "")
key |
Census API Key as a character |
logical if has
, key if get
has_census_key()
has_census_key()
Searches for an exact match and offers the best match if no exact match
match_abb(state)
match_abb(state)
state |
character with state FIPS, Abbreviation, Name, or ANSI |
Abbreviation if a match is found or character(0) if no match is found
match_abb('NY') match_abb('01')
match_abb('NY') match_abb('01')
Searches for an exact match and offers the best match if no exact match
match_ansi(state)
match_ansi(state)
state |
character with state FIPS, Abbreviation, Name, or ANSI |
ANSI if a match is found or character(0) if no match is found
match_ansi('NY') match_ansi('01')
match_ansi('NY') match_ansi('01')
Searches for an exact match and offers the best match if no exact match
match_fips(state)
match_fips(state)
state |
character with state FIPS, Abbreviation, Name, or ANSI |
FIPS code if a match is found or character(0) if no match is found
match_fips('NY') match_fips('01')
match_fips('NY') match_fips('01')
Searches for an exact match and offers the best match if no exact match
match_name(state)
match_name(state)
state |
character with state FIPS, Abbreviation, Name, or ANSI |
Name if a match is found or character(0) if no match is found
match_name('NY') match_name('01')
match_name('NY') match_name('01')
GEOID: Geographic Identifier
NAME: Name of County
pop: total population
pop_white: total population, Non-Hispanic White
pop_black: total population, Non-Hispanic Black
pop_hisp: total population, Hispanic
pop_aian: total population, Non-Hispanic American Indian and Alaskan Native
pop_asian: total population, Non-Hispanic Asian
pop_nhpi: total population, Non-Hispanic Native Hawaiian and Pacific Islander
pop_other: total population, Non-Hispanic Other
pop_two: total population, Non-Hispanic Two Plus Races
vap: voting age population
vap_white: voting age population, Non-Hispanic White
vap_black: voting age population, Non-Hispanic Black
vap_hisp: voting age population, Hispanic
vap_aian: voting age population, Non-Hispanic American Indian and Alaskan Native
vap_asian: voting age population, Non-Hispanic Asian
vap_nhpi: voting age population, Non-Hispanic Native Hawaiian and Pacific Islander
vap_other: voting age population, Non-Hispanic Other
vap_two: voting age population, Non-Hispanic Two Plus Races
geometry: sf geometry
data('mt_county')
data('mt_county')
sf tibble with one observation for each county in Montana
data('mt_county')
data('mt_county')
Replaces state ansi with state abbreviation
recode_abb_ansi(.data, .ansi)
recode_abb_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state abbreviation
data('stata') stata %>% recode_abb_ansi(ansi)
data('stata') stata %>% recode_abb_ansi(ansi)
Replaces state fips with state abb
recode_abb_fips(.data, .fips)
recode_abb_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state abb
data('stata') stata %>% recode_abb_fips(fips)
data('stata') stata %>% recode_abb_fips(fips)
Replaces state name with state abbreviation
recode_abb_name(.data, .name)
recode_abb_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with abbreviation
data('stata') stata %>% recode_abb_name(name)
data('stata') stata %>% recode_abb_name(name)
Replaces state abbreviation with state ansi
recode_ansi_abb(.data, .abb)
recode_ansi_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbrevaition |
.data with column .abb replaced with state ansi
data('stata') stata %>% recode_ansi_abb(abb)
data('stata') stata %>% recode_ansi_abb(abb)
Replaces state fips with state ansi
recode_ansi_fips(.data, .fips)
recode_ansi_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state ansi
data('stata') stata %>% recode_ansi_fips(fips)
data('stata') stata %>% recode_ansi_fips(fips)
Replaces state name with state ansi
recode_ansi_name(.data, .name)
recode_ansi_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with ansi
data('stata') stata %>% recode_ansi_name(name)
data('stata') stata %>% recode_ansi_name(name)
Replaces state abbreviation with state fips
recode_fips_abb(.data, .abb)
recode_fips_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbrevaition |
.data with column .abb replaced with state name
data('stata') stata %>% recode_fips_abb(abb)
data('stata') stata %>% recode_fips_abb(abb)
Replaces state ansi with state fips
recode_fips_ansi(.data, .ansi)
recode_fips_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state fips
data('stata') stata %>% recode_fips_ansi(ansi)
data('stata') stata %>% recode_fips_ansi(ansi)
Replaces state name with state fips
recode_fips_name(.data, .name)
recode_fips_name(.data, .name)
.data |
data.frame or tibble |
.name |
column with state name |
.data with column .name replaced with fips
data('stata') stata %>% recode_fips_name(name)
data('stata') stata %>% recode_fips_name(name)
Replaces state abbreviation with state name
recode_name_abb(.data, .abb)
recode_name_abb(.data, .abb)
.data |
data.frame or tibble |
.abb |
column with state abbrevaition |
.data with column .abb replaced with state name
data('stata') stata %>% recode_name_abb(abb)
data('stata') stata %>% recode_name_abb(abb)
Replaces state ansi with state name
recode_name_ansi(.data, .ansi)
recode_name_ansi(.data, .ansi)
.data |
data.frame or tibble |
.ansi |
column with state ansi |
.data with column .ansi replaced with state name
data('stata') stata %>% recode_name_ansi(name)
data('stata') stata %>% recode_name_ansi(name)
Replaces state fips with state name
recode_name_fips(.data, .fips)
recode_name_fips(.data, .fips)
.data |
data.frame or tibble |
.fips |
column with state fips |
.data with column .fips replaced with state name
data('stata') stata %>% recode_name_fips(fips)
data('stata') stata %>% recode_name_fips(fips)
tibble with columns:
fips: Federal Information Processing Standards codes
abb: two letter postal abbreviations
name: title case state name
ansi: American National Standards Institute codes
region: Census Regions (for 50 states and D.C.)
division: Census Divisions (for 50 states and D.C.)
data('stata')
data('stata')
tibble with state identifying information
data('stata')
data('stata')