Last active
February 3, 2022 20:55
-
-
Save ayman/c2cb00423993d110851de3c725289d20 to your computer and use it in GitHub Desktop.
Compute the distances between all US Zipcodes in R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-hello can I use it for other countries ? Thank you very much !