Skip to contents

This function converts five-digit ZIP Codes to three-digit ZIP Codes. The first three digits of a ZIP Code are known as the ZIP3 Code, and corresponds to the sectional center facility (SCF) that processes mail for a region.

Usage

zi_convert(.data, input_var, output_var)

Arguments

.data

A data frame containing a column of five-digit ZIP Codes.

input_var

A character scalar specifying the column name with the five-digit ZIP Codes in the data frame.

output_var

Optional; A character scalar specifying the column name to store the three-digit ZIP Codes in the data frame.

Value

A tibble containing the original data frame with a new column of three-digit ZIP Codes.

Examples

# add new column
## create sample data
df <- data.frame(id = c(1:3), zip5 = c("63005", "63139", "63636"))

## convert ZIP Codes to ZIP3, creating a new column
zi_convert(.data = df, input_var = zip5, output_var = zip3)
#> Warning: The given 'output_var' column, 'zip5', was found in your data object, and the column was overwritten.
#> # A tibble: 3 × 2
#>      id zip5 
#>   <int> <chr>
#> 1     1 630  
#> 2     2 631  
#> 3     3 636  

# overwrite existing column
## create sample data
df <- data.frame(id = c(1:3), zip = c("63005", "63139", "63636"))

## convert ZIP Codes to ZIP3, creating a new column
zi_convert(.data = df, input_var = zip)
#> # A tibble: 3 × 2
#>      id zip  
#>   <int> <chr>
#> 1     1 630  
#> 2     2 631  
#> 3     3 636