Wow, this package has a some undocumented features which conflict with the help pages. These functions are not vectorized and thus one is required to loop through the dataset line-by-line to obtain the correct answer.
#Sample data
zip_a <- c("94063")
zip_b <- c("94306", "94301", "94030", "94030", "95409")
Zip_Codes_Only <- data.frame(zip_a, zip_b)
library(zipcodeR)
#loop through each row of the dataset and return the calculated distance
distance <- sapply( 1:nrow(Zip_Codes_Only), function(i) {
temp <- zip_distance(Zip_Codes_Only$zip_a[i], Zip_Codes_Only$zip_b[i], units = "miles")
temp$distance
})
#merge distance to original dataset
cbind(Zip_Codes_Only, distance)
Example of the logic error generated by the trying to process a vector with the zip_distance()
function. As per the help page:
zip_distance(zipcode_a, zipcode_b, lonlat = TRUE, units = "miles")
Arguments:
zipcode_a First vector of ZIP codes
zipcode_b Second vector of ZIP codes
zip_c <- c("94306", "94301")
zip_distance(zip_a, zip_c, units = "miles")
# zipcode_a zipcode_b distance
# 1 94063 94306 5.29
# 2 94063 94301 7.61
zip_d <- c("94301", "94306")
zip_distance(zip_a, zip_d, units = "miles")
# zipcode_a zipcode_b distance
# 1 94063 94301 5.29
# 2 94063 94306 7.61
Notice zip_d is a reverse ordering of zip_c but the distance vector in the resulting data frame is the same.