R/validate_state.R

Defines functions validate_state

Documented in validate_state

#' Validate that a abbreviation is correct
#'
#' @param us_state State abbreviation (e.g. NY)
#'
#' @return TRUE if correct FALSE otherwise
#' @import stringr
#' @export



validate_state <- function(us_state){
  if(!is.character(us_state))
    warning('State Abbreviation should be a character')
  # Catch uppercase/lowercase error
  if(toupper(us_state)!=us_state)
    warning('State Abbreviation should be upper case.')
  if(stringr::str_length(us_state)!=2)
    warning('State abbreviations should be two letters.')
  
  state_abb <- c("AL","AK","AZ","AR","CA",
                 "CO","CT","DE","FL","GA","HI","ID",
                 "IL","IN","IA","KS","KY","LA","ME","MD",
                 "MA","MI","MN","MS","MO","MT","NE","NV",
                 "NH","NJ","NM","NY","NC","ND","OH","OK",
                 "OR","PA","RI","SC","SD","TN","TX","UT",
                 "VT","VA","WA","WV","WI","WY")
  us_state%in%state_abb
}

Try the ProPublicaR package in your browser

Any scripts or data that you put into this service are public.

ProPublicaR documentation built on Sept. 8, 2023, 5:53 p.m.