Create or use a named vector (c("full" = "abb")) and pass it to stringr::str_replace_all(). The full argument is surrounded with \\b to capture only isolated intended full versions. Note that the built-in usps_street, usps_city, and usps_state dataframes have the columns reversed from what this function needs (to work by default with the counterpart expand_abbrev()).

abbrev_full(x, full = NULL, rep = NULL, end = FALSE)

Arguments

x

A vector containing full words.

full

One of three objects: (1) A dataframe with full strings in the first column and corresponding abbreviations in the second column; (2) a named vector, with full strings as names for their respective abbreviations (e.g., c("full" = "abb")); or (3) an unnamed vector of full words with an unnamed vector of abbreviations in the rep argument.

rep

If full is an unnamed vector, a vector of abbreviations strings for each full word in abb.

end

logical; if TRUE, then the $ regular expression will be used to only replace words at the end of a string (such as "ROAD" in a street address). If FALSE (default), then the \b regular expression will target all instances of full to be replaced with rep.

Value

The vector x with full words replaced with their abbreviations.

See also

Other geographic normalization functions: abbrev_state(), check_city(), expand_abbrev(), expand_state(), fetch_city(), normal_address(), normal_city(), normal_state(), normal_zip(), str_normal()

Examples

abbrev_full("MOUNT VERNON", full = c("MOUNT" = "MT"))
#> [1] "MT VERNON"
abbrev_full("123 MOUNTAIN ROAD", full = usps_street)
#> [1] "123 MTN RD"
abbrev_full("123 MOUNTAIN ROAD", full = usps_street, end = TRUE)
#> [1] "123 MOUNTAIN RD"
abbrev_full("Vermont", full = state.name, rep = state.abb)
#> [1] "VT"