R Message: error - replacement has x rows, data has y

I am trying to use a packet ggmapto request a distance for a destination address with a list of addresses. My csv data looks like this:

Order ID    Address
1652049  435 E 70TH ST,10021
1652123  1365 YORK AVE,10021
1652053  530 E 72ND ST,10021

so I'm trying to get the distance from my input address to all of these addresses, for example: 400 Hudson St, 10013, and I have the following code in R:

library(ggmap)
mydata<-read.csv("address.csv")
mydata$Address<-as.character(mydata$Address)
mydata$Distance<-NA
a<-c("289 Hudson St,10013")
mydata$Distance<-mapdist(mydata$Address,a)$miles

However, the code gives me an error message as shown below:

Error in `$<-.data.frame`(`*tmp*`, "Distance", value = c(8.2403854, 8.2403854,  : 
  replacement has 53 rows, data has 31
+2
source share
1 answer

Make sure column names do not have spaces; therefore, instead of the name "Order ID", use something like "Order_ID". Also, each address should be a separate separate line:

library(ggmap)

mydata$Address<-as.character(mydata$Address)
mydata$Distance<-NA
a<-c("289 Hudson St,10013")
mydata$Distance<-mapdist(mydata$Address,a)$miles

Output:

  Order_ID             Address Distance
1  1652049 435 E 70TH ST,10021 8.240385
2  1652123 1365 YORK AVE,10021 8.475275
3  1652053 530 E 72ND ST,10021 8.618197

:

mydata <- data.frame(Order_ID=c(1652049,1652123,1652053),
                     Address=c('435 E 70TH ST,10021','1365 YORK AVE,10021',
                               '530 E 72ND ST,10021'))

EDIT:

, ​​ c(). , . , , CSV, , . CSV R, , , , , / , (.. ).

+2

All Articles