Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
postal code loop
# dplyr::case_when to find and clean FSA
#
# Notes:
# * FSA = "Forward Sortation Area" in Canadian postal parlance
# * the regex finds British Columbia FSAs (starting with "V")
FSA_list <- df %>%
mutate(FSA_clean = case_when(
str_detect(FSA, "V\\d.$") == TRUE ~ FSA,
TRUE ~ NA_character_
)) %>%
select(FSA, FSA_clean)
# synthetic postal codes: process to add all possible combinations of digit-character-digit to a single FSA
FSA <- "V9M"
# declare output as character vector
output <- vector("character", 1)
x <- 1 # initialize counter
for (i in 0:9) {
for (j in LETTERS) {
for (k in 0:9) {
output[x] <- (paste(i, j, k, sep = ""))
x <- x + 1
}
}
}
outtibble <- as_tibble(output)
head(outtibble)
tail(outtibble)
outtibble %>%
mutate(postcode = paste(FSA, value, sep = ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.