Finding the nearest zipcode from a list of zipcodes - r

I have a list of locations with zipcodes. I have another list of Distribution Centers that serve these locations. Is there anyway to map the nearest DC to each of these locations? I am an extremely green coder, but i have some experience with R

I'd need more information to give you some possible code to solve your problem however, here is one approach to solving your problem.
Convert your zipcodes to longitudes and latitudes.
Not sure what location data you have on your distribution centers, but you should be able to find a way to retrieve the long/lat of each of these.
For each zipcode, compute the the distance to each DC (using their respective longs/lats). To compute the distance, use the haversine formula. Find the minimum of these distances. This is your solution.

Related

A* algorithm when the heuristic can not be calculated for some nodes

I am working on a dataset of cities and towns spread across North America with the objective of finding the shortest path between a starting point and an ending point. I decided to use the Haversine distance as my heuristic function. But, my dataset doesn't have the latitude and longitude coordinates for some of the towns that could lie in the shortest distance path. How am I supposed to calculate the heuristic in this case? Would taking the average of the heuristics of the neighboring towns make sense?
It is given that a town/city without its corresponding coordinates cant be the starting point or the ending point.
Is there a different heuristic I should be considering instead of the Haversive distance?
If I remember correctly (don’t trust me on this!) a heuristic that returns zero for some nodes is still "legal" (in the sense that when you get to the end node, you know it’s optimal), so that would be a brutal solution. Obviously, doing this for too many nodes would wreck your search performance!
I think that interpolating between neighbour locations risks creating an inadmissible heuristic.

Cluster by distance between a lot off point in R

I need to create clusters from the distance between clients if the distance between the points is less than certain precise value group them together.
I thought about using Delaunay but I'm not having success

R: Matching closest coordinates for large data set

I have two sets of data - the first is a list of lat/long coordinates for 2,500 sites where trees have been measured, and the second is a list of lat/long coordinates for 88 temperature monitoring sites.
I want to match each of the 2,500 sites in to its temperature monitoring site.
what i have so far is
distance=geodists(lat.coord.A,long.coord.B,lat.coordB,long.coordB, K)
to calculate the distance between a site in data.set.A and data.set.B, and am looking into using the apply functions to get r perform this for each of the 88 temp. sites at once.
i'm then playing with using min() to give the smallest distance from the site in data.set.A to any of those in data.set.B, but i'd rather just get the coordinates of that specific site in data.set.B than have to calculate it myself.
I'm sure this can be done relatively simply but can't seem to get it right.
I'm pretty new to R so any help is very much appreciated!
You are looking something like (using data.table):
Do a cartesian join:
CartesianJoin<- function(X,Y)
setkey(X[,c(k=1,.SD)],k)[Y[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]
LatLonWide <- CartesianJoin(data.set.A,data.set.B)
Then calculate distance using:
LatLonWide$dist <- sapply(1:nrow(LatLonWide),function(i)
geodists(LatLonWide$lat.coord.A[i],LatLonWide$long.coord.A[i],LatLonWide$lat.coord.B[i],LatLonWide$long.coord.B[i]))

Correctly compare areas from multiple parts of the globe using longitude and latitude

Here's my problem. I want to compare the area within multiple polygons in different parts of the world. I have the longitude and latitudes for each point of each polygon. My problem is that I don't know what projection to use to get x-y coordinates from the long-lat coordinates. I know OpenStreetMap has the projectMercator() function, but areas are known to inflate quite badly with latitude. (http://en.wikipedia.org/wiki/List_of_map_projections)
--> Do you guys know of an R function like projectMercator, that doesn't have such a distortion? I've been going over different types of projections in Wikipedia, but it's very unclear to me which is best for area comparisons, and then if those projections exist in R as functions (if they don't I'm fine hand coding them, though!)
Thanks!!!
Hillary

Approaches for spatial geodesic latitude longitude clustering in R -- Follow-Up

Mine are follow-ups to the question & answer in Approaches for spatial geodesic latitude longitude clustering in R with geodesic or great circle distances.
I would like to better understand:
Question #1: If all the lat / long values are within the same city, is it necessary to use either fossil or distHaversine(...) to first calculate great circle distances ?
or, within a single city, is it OK to run clustering on the lat/long values themselves ?
Question #2: jlhoward suggests that :
It's worth noting that these methods require that all points must go into some cluster. If you just ask which points are close together, and allow that some cities don't go into any cluster, you get very different results.
In my case I would like to ask just ask "which points are close together", without forcing every point into a cluster. How can I do this ?
Question #3: To include one or two factor variables into the clustering (in addition to lat/long), is it as easy as including those factor variables in the df upon which the clustering is run ?
Please confirm.
Thanks!
"within a single city, is it OK to run clustering on the lat/long values themselves ?"
Yes, as long as your city is on the equator, where a degree of longitude is the same distance as a degree of latitude.
I'm standing very close to the north pole. One degree of longitude is 1/360 of the circumference of the circle round the pole from me. Someone ten degrees east of me might only be ten feet away. Someone one degree south of me is miles away. A clustering algorithm based on lat-long would think that guy miles away was closer to me than the guy I can wave to ten degrees east of me.
The solution for small areas to save having to compute great-circle ellipsoid distances is to project to a coordinate system that is near-enough cartesian so that you can use pythagoras' theorem for distance without too much error. Typically you would use a UTM zone transform, which is essentially a coordinate system that puts its equator through your study area.
The spTransform function in sp and rgdal will sort this out for you.

Resources