Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# (?<= ) means "look for a space before this, but don't include it in what we
# extract."
# Square brackets mean "look for any of these", so [a-z() ] means "look for
# lowercase letters, parentheses, or spaces."
# The plus sign means "find at least one of these."
# The dollar sign means "this has to occur at the end of the text."
target <- "(?<= )[a-z() ]+$"
commute_mode %>%
dplyr::mutate(
type = dplyr::if_else(
stringr::str_detect(city, target),
stringr::str_extract(city, target),
"city"
),
city = stringr::str_trim(stringr::str_replace(city, type, ""))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment