问题
I am getting this error in R when I try to run the function route() within a loop:
Error: (list) object cannot be coerced to type 'integer'
The function is found in the package "ggmap". It takes two strings as an argument and searches on google maps the directions between them. It returns a dataframe which contains information about the route including distance, minutes, coordinates etc.
I have a vector with many strings which represent postcodes and I am using this route() function within a loop on those postcodes. It runs ok for the first 40 or so searches, but then it crashes giving the above error.
df <- as.data.frame(read.csv("df.csv", sep=","))
for (i in df$post_code){
route(i,'NE29 8NH', structure = 'route', mode = 'driving')
}
I checked the vector with postcodes and there are no missing values or other anomalies. Please help me with this.
回答1:
I believe the issue has to do with google maps rate limits, just enter the request URL and refresh the browser a few times and you will get the below message:
{
"error_message" : "You have exceeded your rate-limit for this API.",
"routes" : [],
"status" : "OVER_QUERY_LIMIT"
}
ggmaps tries to parse the json response, but the response doesn't have any information. I would suggest forcing a delay in your loop with Sys.sleep(x), x<-seconds.
来源:https://stackoverflow.com/questions/25083512/error-list-object-cannot-be-coerced-to-type-integer