Determining the distance between two ZIP codes (alternatives to mapdist)
taRifx.geo::georoute
(only available here until I push out another update, at which point it will be available via install.packages
) can use Bing Maps (which supports I believe 25k per day) and can return a distance.
georoute( c("3817 Spruce St, Philadelphia, PA 19104",
"9000 Rockville Pike, Bethesda, Maryland 20892"),
verbose=TRUE, returntype="time",
service="bing" )
You'll have to get a Bing Maps API key and set it in your R global options (ideal placement is in .Rprofile
), but the key is free:
options(BingMapsKey="whateverBingGivesYouForYourKey")
Calculating the distance between ZIP Codes in R with the mapdist function
It could be that the zip code on its own is ambiguous. If you include 'USA' in the search string it works
library(ggmap)
mapdist(from = c("19111, USA"), to = c("19187, USA"))
# from to m km miles seconds minutes hours
# 1 19111, USA 19187, USA 21420 21.42 13.31039 1976 32.93333 0.5488889
Or
library(googleway)
set_key("your_api_key")
google_distance(origins = c("19111, USA"),
destinations = c("19187, USA"))
# $destination_addresses
# [1] "Philadelphia, PA 19187, USA"
#
# $origin_addresses
# [1] "Philadelphia, PA 19111, USA"
#
# $rows
# elements
# 1 21.4 km, 21420, 33 mins, 1976, 35 mins, 2101, OK
#
# $status
# [1] "OK"
Determining the distance between multiple ZIP codes from one point
library(ggmap)
Building an example data.frame
geoData <- data.frame(FROM = c('95077', 'Manchester Deaf Institute'),
TO = c('06473', 'Birmingham O2 Academy 1'),
stringsAsFactors = FALSE)
passing columns as args
mapdist(from = geoData[['FROM']],
to = geoData[['TO']],
mode = 'driving')
result
from to m km miles seconds minutes hours
1 95077 06473 4932333 4932.333 3064.95173 161558 2692.6333 44.877222
2 Manchester Deaf Institute Birmingham O2 Academy 1 141330 141.330 87.82246 6569 109.4833 1.824722
How to calculate Distance between Two Zipcodes in Miles Using R
You can find online a table that has longitudes and latitudes for 29,000+ US cities, along with their zip codes. with it you could then convert your columns of zip codes to coordinates.
Then, the geosphere
package offers eight functions for calculating distances (great circle, Haversine, Rhumb, etc) based on coordinates. It also converts meters to miles.
Distance between two zipcodes using zipcodeR package not working
The zip code "32610"
is not valid as per is_zcta
(which returns true if the given ZIP code is also a ZIP code tabulation area)
is_zcta("32610")
[1] FALSE
Use mapdist() to calculate distance between beijing and new york
If you look at R's help option it gives you a nice example of what is going on.
#import ggmap
library(ggmap)
#get set of longitudinal and latitudinal coordinates
(wh <- as.numeric(geocode("the white house, dc")))
(lm <- as.numeric(geocode("lincoln memorial washington dc")))
You have to also set a mode for walking, bicycling or driving.
mapdist(wh, lm, mode = "walking")
As Alistaire mentions, for geographic distances, you must calculate distances according to the earth's spherical shape. Even though the earth isn't perfectly circular, there are formulas that are reasonably accurate.
The following is a formula I took directly from this website:
http://www.r-bloggers.com/great-circle-distance-calculations-in-r/
gcd.hf <- function(long1, lat1, long2, lat2) {
R <- 6371 # Earth mean radius [km]
delta.long <- (long2 - long1)
delta.lat <- (lat2 - lat1)
a <- sin(delta.lat/2)^2 + cos(lat1) * cos(lat2) * sin(delta.long/2)^2
c <- 2 * asin(min(1,sqrt(a)))
d = R * c
return(d) # Distance in km
}
I'd read the link if this you are asking for geographical distance.
Related Topics
Automatic Documentation of Datasets
Data.Table Alternative for Dplyr Case_When
Ggplot2:Adding Two Errorbars to Each Point in Scatterplot
Colorize Clusters in Dendogram with Ggplot2
Debugging (Line by Line) of Rcpp-Generated Dll Under Windows
Cannot Coerce Type 'Closure' to Vector of Type 'Character'
Extracting Noun+Noun or (Adj|Noun)+Noun from Text
R:Ggplot2:Facet_Grid:How Include Math Expressions in Few (Not All) Labels
How to Add a Line to One of the Facets
How to Add an External Legend to Ggpairs()
Fast Replacing Values in Dataframe in R
Determining the Distance Between Two Zip Codes (Alternatives to Mapdist)
Reversed Order After Coord_Flip in R
How to Change Font Size of the Correlation Coefficient in Corrplot
Summing Across Rows of a Data.Table for Specific Columns
Remove Plot Margins in Ggplot2
Basic - T-Test -> Grouping Factor Must Have Exactly 2 Levels
Assign New Data Point to Cluster in Kernel K-Means (Kernlab Package in R)