"non-numeric argument to binary operator" 在 ggmap 中
"non-numeric argument to binary operator" in ggmap
这个问题已经更新了好几次了。
这是 的进一步问题。我真的无法理解错误信息。我有两个数据框:
> route1
X1 X2 X3
1 Balboa Park, san diego, ca Coronado, san diego, ca USS Midway, san diego, ca
2 Balboa Park, san diego, ca USS Midway, san diego, ca Coronado, san diego, ca
3 Coronado, san diego, ca Balboa Park, san diego, ca USS Midway, san diego, ca
4 Coronado, san diego, ca USS Midway, san diego, ca Balboa Park, san diego, ca
5 USS Midway, san diego, ca Balboa Park, san diego, ca Coronado, san diego, ca
6 USS Midway, san diego, ca Coronado, san diego, ca Balboa Park, san diego, ca
和
> place
# A tibble: 8 x 5
name hour type short abbr
* <chr> <dbl> <chr> <chr> <chr>
1 Balboa Park, san diego, ca 4 place Balboa Park bal
2 USS Midway, san diego, ca 2 place USS Midway midway
3 Coronado, san diego, ca 4 place Coronado coro
4 Cabrillo National Monument, san diego, ca 2 place Cabrillo cab
5 La Jolla Cove, san diego, ca 2 place La Jolla ljc
6 4002 Wallace St, San Diego, CA 92110 4 place Old Town ot
7 Hotel Republic San Diego, Autograph Collection NA hotel Autograph Collection NA
8 THE US GRANT, a Luxury Collection Hotel, San Diego NA hotel Luxury Collection NA
我的想法是使用ggmap
包来获得从Hotel Republic San Diego, Autograph Collection
(place$name[7]
)到route1
第一行的第一位的行驶时间,然后开始行驶从route1
第一行第一位到route1
第一行第二位的时间,以此类推,直到从route1
第一行最后一位到THE US GRANT, a Luxury Collection Hotel, San Diego
(place$name[8]
)。总结时间,存入route1$time
列。然后对以下行执行相同的操作。所以我的代码是
library(ggmap)
register_google(key = "my_google_key_here")
route1$time <- 0
for (i in 1:length(route1)) {
for (j in 1:length(route1[i,])) {
if(j == 1){
route1$time[i] <- route1$time[i] + mapdist(from = place$name[7], to = as.character(route1[i,][1]))$seconds
}
if(j == length(route1[i,])){
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j]), to = place$name[8])$seconds
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j - 1]), to = as.character(route1[i,][j]))$seconds
}
for (j in 2:length(route1[i,]-1)) {
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j - 1]), to = as.character(route1[i,][j]))$seconds
}
}
}
执行这段代码给我
Error in FUN(left, right) : non-numeric argument to binary operator
报错信息,好像是说我给数字加了一个字符,但是找不到。
# this is a nice library for R
library(tidyverse)
# name your variables
# (you can use these as arguments to a function or something)
start = place$name[7]
end = place$name[8]
# wrap your computation into a function which takes
# a row of a data frame as input
compute_route_time = function(row) {
# get a vector of stops that you can loop through
stops = unlist(row)
# distance from start to first stop
time = mapdist(from = start, to = stops[1])$seconds
# iterate through the rest of the route
n_stops = length(stops)
for (i in 1:(n_stops-1)) {
time = time + mapdist(from = stops[i], to = stops[i+1])$seconds
}
# finish rout
time = time + mapdist(from = stops[n_stops], to = end)$seconds
# collect row as a data frame to preserve old columns
row = as.data.frame(row)
# add time column
row$time = time
return(row)
}
route1 %>%
dplyr::rowwise() %>%
dplyr::do(compute_route_time(.))
这个问题已经更新了好几次了。
这是
> route1
X1 X2 X3
1 Balboa Park, san diego, ca Coronado, san diego, ca USS Midway, san diego, ca
2 Balboa Park, san diego, ca USS Midway, san diego, ca Coronado, san diego, ca
3 Coronado, san diego, ca Balboa Park, san diego, ca USS Midway, san diego, ca
4 Coronado, san diego, ca USS Midway, san diego, ca Balboa Park, san diego, ca
5 USS Midway, san diego, ca Balboa Park, san diego, ca Coronado, san diego, ca
6 USS Midway, san diego, ca Coronado, san diego, ca Balboa Park, san diego, ca
和
> place
# A tibble: 8 x 5
name hour type short abbr
* <chr> <dbl> <chr> <chr> <chr>
1 Balboa Park, san diego, ca 4 place Balboa Park bal
2 USS Midway, san diego, ca 2 place USS Midway midway
3 Coronado, san diego, ca 4 place Coronado coro
4 Cabrillo National Monument, san diego, ca 2 place Cabrillo cab
5 La Jolla Cove, san diego, ca 2 place La Jolla ljc
6 4002 Wallace St, San Diego, CA 92110 4 place Old Town ot
7 Hotel Republic San Diego, Autograph Collection NA hotel Autograph Collection NA
8 THE US GRANT, a Luxury Collection Hotel, San Diego NA hotel Luxury Collection NA
我的想法是使用ggmap
包来获得从Hotel Republic San Diego, Autograph Collection
(place$name[7]
)到route1
第一行的第一位的行驶时间,然后开始行驶从route1
第一行第一位到route1
第一行第二位的时间,以此类推,直到从route1
第一行最后一位到THE US GRANT, a Luxury Collection Hotel, San Diego
(place$name[8]
)。总结时间,存入route1$time
列。然后对以下行执行相同的操作。所以我的代码是
library(ggmap)
register_google(key = "my_google_key_here")
route1$time <- 0
for (i in 1:length(route1)) {
for (j in 1:length(route1[i,])) {
if(j == 1){
route1$time[i] <- route1$time[i] + mapdist(from = place$name[7], to = as.character(route1[i,][1]))$seconds
}
if(j == length(route1[i,])){
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j]), to = place$name[8])$seconds
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j - 1]), to = as.character(route1[i,][j]))$seconds
}
for (j in 2:length(route1[i,]-1)) {
route1$time[i] <- route1$time[i] + mapdist(from = as.character(route1[i,][j - 1]), to = as.character(route1[i,][j]))$seconds
}
}
}
执行这段代码给我
Error in FUN(left, right) : non-numeric argument to binary operator
报错信息,好像是说我给数字加了一个字符,但是找不到。
# this is a nice library for R
library(tidyverse)
# name your variables
# (you can use these as arguments to a function or something)
start = place$name[7]
end = place$name[8]
# wrap your computation into a function which takes
# a row of a data frame as input
compute_route_time = function(row) {
# get a vector of stops that you can loop through
stops = unlist(row)
# distance from start to first stop
time = mapdist(from = start, to = stops[1])$seconds
# iterate through the rest of the route
n_stops = length(stops)
for (i in 1:(n_stops-1)) {
time = time + mapdist(from = stops[i], to = stops[i+1])$seconds
}
# finish rout
time = time + mapdist(from = stops[n_stops], to = end)$seconds
# collect row as a data frame to preserve old columns
row = as.data.frame(row)
# add time column
row$time = time
return(row)
}
route1 %>%
dplyr::rowwise() %>%
dplyr::do(compute_route_time(.))