Learn R Programming

zippeR (version 0.1.0)

zi_validate: Validate ZIP Code or ZCTA Vector

Description

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)

Value

Either a logical value (if verbose = FALSE) or a tibble

containing validation criteria and results.

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.

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

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

# successful validation
zi_validate(zips)

# 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)

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

# failed validation
zi_validate(zips)
zi_validate(zips, verbose = TRUE)

Run the code above in your browser using DataLab