Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Last active July 22, 2020 23:50
Show Gist options
  • Save MonkmanMH/a23a0805a7d6317d9f1440fb2b14fc31 to your computer and use it in GitHub Desktop.
Save MonkmanMH/a23a0805a7d6317d9f1440fb2b14fc31 to your computer and use it in GitHub Desktop.
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