-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also function for joining state reference data
- Loading branch information
1 parent
0de6e5f
commit 380c1b4
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Join additional attribute columns: Census division, region, and USPS abbreviation to Census state column | ||
#' @noRd | ||
join_us_census_state <- function(data) { | ||
stopifnot( | ||
all(has_name(data, "STATE")) | ||
) | ||
|
||
data |> | ||
dplyr::left_join(usa_states, by = "STATE") | ||
} | ||
|
||
|
||
#' Format NHGIS places data by joining additional attributes and adding a label | ||
#' column | ||
#' | ||
#' [fmt_nhgis_places()] joins Census division, region, and USPS abbreviation | ||
#' columns based on the state value and creates a new label column following the | ||
#' pattern of "<NAME column with remove pattern applied>, <USPS State | ||
#' Abbreviation>". | ||
#' | ||
#' @param name_col Name column to use as the basis for the label. | ||
#' @param label_col Label column name. | ||
#' @param remove_pattern Passed to `pattern` for [stringr::str_remove()] | ||
#' @keywords internal fmt | ||
#' @export | ||
fmt_nhgis_places <- function(data, | ||
label_col = "label", | ||
name_col = "NAME", | ||
remove_pattern = " city$") { | ||
stopifnot( | ||
all(rlang::has_name(data, name_col)), | ||
!rlang::has_name(data, label_col) | ||
) | ||
|
||
data |> | ||
join_us_census_state() |> | ||
dplyr::mutate( | ||
"{label_col}" := stringr::str_remove(.data[[name_col]], remove_pattern), | ||
"{label_col}" := paste0(.data[[label_col]], ", ", STUSPS) | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ | |
} | ||
|
||
utils::globalVariables( | ||
c("GISJOIN", "DATAYEAR") | ||
c("GISJOIN", "DATAYEAR", "STUSPS") | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.