Skip to content

Instantly share code, notes, and snippets.

@hakimelek
Last active January 30, 2017 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakimelek/c10bd93ec5dfa284947860e317223a42 to your computer and use it in GitHub Desktop.
Save hakimelek/c10bd93ec5dfa284947860e317223a42 to your computer and use it in GitHub Desktop.
Get distance from a list of zipcodes
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