0

In a helpful post from Dave2e back in 2019, I'm using his suggested function as described here:

R Find the Distance between Two US Zipcode columns

Dave's solution works great for the OP. My issue is: When I feed the function more than 500 rows of starting & ending zip data, I get this error below

Error in .pointsToMatrix(p1) : Wrong length for a vector, should be 2

Using 500 rows is OK! But if I feed the function any more than 500 rows-- it throws the error above.

Question: Is it possible to outmaneuver this limitation by re-writing the given function? If so, could you please explain & guide the way? Thank you!

library(zipcode)
library(geosphere)

#dataframe need to be character arrays or the else the leading zeros will be dropped causing errors
df <- data.frame("ZIP_START" = c(95051, 94534, 60193, 94591, 94128, 94015, 94553, 10994, 95008), 
       "ZIP_END" = c(98053, 94128, 60666, 73344, 94128, 73344, 94128, "07105", 94128), 
       stringsAsFactors = FALSE)

data("zipcode")

df$distance_meters<-apply(df, 1, function(x){
  startindex<-which(x[["ZIP_START"]]==zipcode$zip)
  endindex<-which(x[["ZIP_END"]]==zipcode$zip)
  distGeo(p1=c(zipcode[startindex, "longitude"], zipcode[startindex, "latitude"]), p2=c(zipcode[endindex, "longitude"], zipcode[endindex, "latitude"]))
})
user5509057
  • 159
  • 6

0 Answers0