Skip to content

Commit

Permalink
Add place formatting function
Browse files Browse the repository at this point in the history
Also function for joining state reference data
  • Loading branch information
elipousson committed May 29, 2024
1 parent 0de6e5f commit 380c1b4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
41 changes: 41 additions & 0 deletions R/fmt.R
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)
)
}
2 changes: 1 addition & 1 deletion R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
}

utils::globalVariables(
c("GISJOIN", "DATAYEAR")
c("GISJOIN", "DATAYEAR", "STUSPS")
)
29 changes: 29 additions & 0 deletions man/fmt_nhgis_places.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 380c1b4

Please sign in to comment.