Skip to content

Instantly share code, notes, and snippets.

@ayman
Last active February 3, 2022 20:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayman/c2cb00423993d110851de3c725289d20 to your computer and use it in GitHub Desktop.
Save ayman/c2cb00423993d110851de3c725289d20 to your computer and use it in GitHub Desktop.
Compute the distances between all US Zipcodes in R.
## Compute the distances between all US Zipcodes.
## To save time we just compute the diagonal and use multiple cores.
require(parallel)
library(geosphere)
library(MASS)
library(zipcode)
data(zipcode)
## Uncode for testing
## zipcode <- zipcode[1:10,]
zips.num = dim(zipcode)[1]
zips.range = 1:zips.num
## d <- matrix(0, zips.num, zips.num)
computeDists <- function(x) {
cat(paste("Row", x, paste("(", zipcode$zip[x], ")", sep=""), "\n"))
m <- matrix(0, 1, zips.num)
for (j in x:zips.num) {
# cat(paste(x, j, "\n"))
m[1, j] <- distHaversine(zipcode[j, c(5,4)],
zipcode[x, c(5,4)])
}
return(m)
}
l <- mclapply(zips.range, computeDists, mc.cores=4, mc.preschedule=TRUE)
dd <- do.call(rbind, l)
dimnames(dd) <- list(zipcode$zip, zipcode$zip)
write.matrix(dd, file="zipdist.tsv", sep="\t")
@claudianavarro
Copy link

-hello can I use it for other countries ? Thank you very much !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment