Last active
January 30, 2017 08:03
-
-
Save hakimelek/c10bd93ec5dfa284947860e317223a42 to your computer and use it in GitHub Desktop.
Get distance from a list of zipcodes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
returnDistanceFromVector = function(origin, listOfZipcodes){ | |
library(geosphere) | |
library(ggmap) | |
originCoord = geocode(as.character(origin)) | |
result = data.frame(Zipcode=numeric(), distance= numeric()) | |
for(i in 1:length(listOfZipcodes)) | |
{ | |
if ( !is.na(listOfZipcodes[i])) { | |
destCoord = geocode(as.character(listOfZipcodes[i])) | |
distance = distm (originCoord, destCoord, fun = distHaversine)[1,1] | |
result= rbind(result, c(distance, listOfZipcodes[i])) | |
} | |
else { | |
distance= NA | |
result= rbind(result, c(distance, listOfZipcodes[i])) | |
} | |
names(result) <- c("distance", "zipcode") | |
} | |
return(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment