[R] finding nearest zip codes

Debasmita Sur d@071185 @end|ng |rom gm@||@com
Tue Aug 4 21:03:13 CEST 2020


Dear R-experts,
I have two lists of US zip codes and want to pick the nearest zip code from
second list against my first list.e.g.30043 (from second list) is closest
to the zip code 30094 (from first list).So,it should come against 30094.The
code should compare the distance from each zip and pick the nearest one.
I have written the following code. It is giving proper results for many,
but in mindist, it is showing 'NAs'. But for some of the zip codes, it is
giving proper minimum distance. Please note it will be effective for 5
digit zip codes. Any help will be highly appreciated.

df1<-read.csv("C:/Users/dxsur/Desktop/ZIP1.csv")
df2<-read.csv("C:/Users/dxsur/Desktop/ZIP2.csv")

results<-merge(x=df1,y=zipcode,all.x=TRUE)
results1<-merge(x=df2,y=zipcode,all.x=TRUE)
distance<-distm(subset(results,select=c(longitude,latitude)),subset(results1,select=c(longitude,latitude)))

rnum=apply(distance, 1, which.min)
mindist=apply(distance, 1, min)

final<-cbind(results,results1$zip[unlist(rnum)],mindist)


Thanks & Regards,
*Debasmita *

	[[alternative HTML version deleted]]



More information about the R-help mailing list