Skip to contents

This function validates vectors of ZIP Code or ZCTA values. It is used internally throughout zippeR for data validation, but is exported to facilitate troubleshooting.

Usage

zi_validate(x, style = "zcta5", verbose = FALSE)

Arguments

x

A vector containing ZIP or ZCTA values to be validated.

style

A character scalar - either "zcta5" (default) or "zcta3".

verbose

A logical scalar; if FALSE (default), an overall evaluation will be returned. If TRUE, a tibble object listing validation criteria and results will be returned.

Value

Either a logical value (if verbose = FALSE) or a tibble containing validation criteria and results.

Details

The zi_validate() function checks for four conditions:

  • Is the input vector character data? This is important because of USPS's use of leading zeros in ZIP codes and ZCTAs.

  • Are all values five characters (if style = "zcta5", the default), or three characters (if style = "zcta3")?

  • Are any input values over five characters (if style = "zcta5", the default), or three characters (if style = "zcta3")?

  • Do any input values have non-numeric characters?

The questions provide a basis for repairing issues identified with zi_repair().

Examples

# sample five-digit ZIPs
zips <- c("63088", "63108", "63139")

# successful validation
zi_validate(zips)
#> [1] TRUE

# sample five-digit ZIPs in data frame
zips <- data.frame(id = c(1:3), ZIP = c("63139", "63108", "00501"), stringsAsFactors = FALSE)

# successful validation
zi_validate(zips$ZIP)
#> [1] TRUE

# sample five-digit ZIPs with character
zips <- c("63088", "63108", "zip")

# failed validation
zi_validate(zips)
#> [1] FALSE
zi_validate(zips, verbose = TRUE)
#> # A tibble: 4 × 2
#>   condition                                   result
#>   <chr>                                       <lgl> 
#> 1 Input is a character vector?                TRUE  
#> 2 All input values have 5 characters?         FALSE 
#> 3 No input values are over 5 characters long? TRUE  
#> 4 All input values are numeric?               FALSE