R/shortenStates.R

#' @title Converts full state name to two letter abbreviation
#' @description Useful for messy data in which some rows contain full state names, and others contain the two letter abbreviation.
#' @param stateColumn The name of the column that has state names in it.
#' @export
shortenStates <- function(stateColumn){
  stateColumn <- case_when(
    grepl("Hawaii", x = stateColumn, ignore.case = T) ~ "HI",
    grepl("Alaska", x = stateColumn, ignore.case = T) ~ "AK",
    grepl("Washington", x = stateColumn, ignore.case = T) ~ "WA",
    grepl("Oregon", x = stateColumn, ignore.case = T) ~ "OR",
    grepl("California", x = stateColumn, ignore.case = T) ~ "CA",
    grepl("Idaho", x = stateColumn, ignore.case = T) ~ "ID",
    grepl("Nevada", x = stateColumn, ignore.case = T) ~ "NV",
    grepl("Arizona", x = stateColumn, ignore.case = T) ~ "AZ",
    grepl("Montana", x = stateColumn, ignore.case = T) ~ "MT",
    grepl("Wyoming", x = stateColumn, ignore.case = T) ~ "WY",
    grepl("Utah", x = stateColumn, ignore.case = T) ~ "UT",
    grepl("New Mexico", x = stateColumn, ignore.case = T) ~ "NM",
    grepl("North Dakota", x = stateColumn, ignore.case = T) ~ "ND",
    grepl("South Dakota", x = stateColumn, ignore.case = T) ~ "SD",
    grepl("Nebraska", x = stateColumn, ignore.case = T) ~ "NE",
    grepl("Kansas", x = stateColumn, ignore.case = T) ~ "KS",
    grepl("Oklahoma", x = stateColumn, ignore.case = T) ~ "OK",
    grepl("Arkansas", x = stateColumn, ignore.case = T) ~ "AK",
    grepl("Texas", x = stateColumn, ignore.case = T) ~ "TX",
    grepl("Minnesota", x = stateColumn, ignore.case = T) ~ "MN",
    grepl("Wisconsin", x = stateColumn, ignore.case = T) ~ "WI",
    grepl("Iowa", x = stateColumn, ignore.case = T) ~ "IA",
    grepl("Missouri", x = stateColumn, ignore.case = T) ~ "MO",
    grepl("Illinois", x = stateColumn, ignore.case = T) ~ "IL",
    grepl("Michigan", x = stateColumn, ignore.case = T) ~ "MI",
    grepl("Indiana", x = stateColumn, ignore.case = T) ~ "IN",
    grepl("Kentucky", x = stateColumn, ignore.case = T) ~ "KY",
    grepl("Tennessee", x = stateColumn, ignore.case = T) ~ "TN",
    grepl("Mississippi", x = stateColumn, ignore.case = T) ~ "MI",
    grepl("Louisiana", x = stateColumn, ignore.case = T) ~ "LA",
    grepl("Ohio", x = stateColumn, ignore.case = T) ~ "OH",
    grepl("Pennsylvania", x = stateColumn, ignore.case = T) ~ "PA",
    grepl("West Virginia", x = stateColumn, ignore.case = T) ~ "WV",
    grepl("Virginia", x = stateColumn, ignore.case = T) ~ "VA",
    grepl("Florida", x = stateColumn, ignore.case = T) ~ "FL",
    grepl("Georgia", x = stateColumn, ignore.case = T) ~ "GA",
    grepl("North Carolina", x = stateColumn, ignore.case = T) ~ "NC",
    grepl("South Carolina", x = stateColumn, ignore.case = T) ~ "SC",
    grepl("District|of Columbia", x = stateColumn, ignore.case = T) ~ "DC",
    grepl("Maryland", x = stateColumn, ignore.case = T) ~ "MD",
    grepl("New Jersey", x = stateColumn, ignore.case = T) ~ "NJ",
    grepl("New York", x = stateColumn, ignore.case = T) ~ "NY",
    grepl("Massachu", x = stateColumn, ignore.case = T) ~ "MA",
    grepl("Vermont", x = stateColumn, ignore.case = T) ~ "VT",
    grepl("Rhode", x = stateColumn, ignore.case = T) ~ "RI",
    grepl("Connecticut", x = stateColumn, ignore.case = T) ~ "CT",
    grepl("New Hampshire", x = stateColumn, ignore.case = T) ~ "NH",
    grepl("Maine", x = stateColumn, ignore.case = T) ~ "ME",
    grepl("Delaware", x = stateColumn, ignore.case = T) ~ "DE",
    grepl("Alabama", x = stateColumn, ignore.case = T) ~ "AL",
    grepl("Colorado", x = stateColumn, ignore.case = T) ~ "CO",
    !is.na(stateColumn) ~ stateColumn
  )
}
RonGuymon/ronsFunctions documentation built on May 8, 2019, 11:42 a.m.