R Message: Error - replacement has x rows, data has y
Asked Answered
B

1

2

I am trying to use the package ggmap to caculate the distance for a target address with a list of addresse. My data in a csv looks like below:

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

so I try to get the distance from my input address to all those address for example: 400 Hudson St,10013, and I have 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 a error message as below:

Error in `$<-.data.frame`(`*tmp*`, "Distance", value = c(8.2403854, 8.2403854,  : 
  replacement has 53 rows, data has 31
Barrick answered 10/9, 2017 at 20:59 Comment(2)
can you provide a reproducible example (with data)?Angi
Sry, Cyrus i don't know how to upload a file, but it is a simple excel cvs file has 2 column, one is ids, another is is address.Barrick
Q
2

Make sure column names don't have spaces; so instead of the name "Order ID", use something like "Order_ID". Also have each address as its own separate string:

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

Sample data:

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:

Note that in the above data, each address is its own string within a vector c(). We can see this is the case by the use of single quotation marks around each address. The reason we do this is to prevent mixing up the data in the case of using CSV files, which have columns that are comma-separated. Before reading a CSV file into R which has commas in its columns, like the address column above, make sure each value/cell in that column is saved as its own string as I've done (i.e. surrounded by single quotes).

Quanta answered 10/9, 2017 at 22:5 Comment(4)
Thank you. i am not sure what is one string means.In your example, you use a vector to combine those addresses, but i am reading a data from cvs file, any idea, which my input is not consider as one string?Barrick
@WorstSQLNoob - Good question. See the edit that adds an explanation above.Quanta
Hello Ryan, i found the problem. in my data, i have same address for different order_id, then it will gives me the error. but if i removed those duplicate addresses, my original code works well.Barrick
@WorstSQLNoob - Okay, good. That can sometimes happen if there are spaces in the column names. So that's why I had included a comment about that in my answer too. Glad this was helpful.Quanta

© 2022 - 2024 — McMap. All rights reserved.