0

This question has been updated several times.

This is a further question from how to create data frames (not just one) at once in r. I really can't understand the error message. I have two data frames:

> 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

and

> 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    

My idea is using ggmap package to get driving time from Hotel Republic San Diego, Autograph Collection (place$name[7]) to the first place in route1's first row, then get driving time from the first place in route1's first row to the second place in route1's first row, and so on, until from the last place in route1's first row to THE US GRANT, a Luxury Collection Hotel, San Diego(place$name[8]). Sum up the time, store in route1$time column. Then do the same thing for the following rows. So my code is

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
    }
  }
}

Executing this code gives me

Error in FUN(left, right) : non-numeric argument to binary operator

error message, I think it is saying that I add a character to a number, but I can't find it.

Community
  • 1
  • 1
vincent
  • 307
  • 1
  • 2
  • 11
  • Do you know why your error message says `time + mapdist(from = place$name[7], to = as.character(route1[i,` but the code you posted says `time[i] + mapdist(from = place$name[7], to = as.character(route1[i,`? – Erin Dec 07 '18 at 01:55
  • @Erin Thank you for bringing it up. I edited my code and updated the question. – vincent Dec 07 '18 at 02:07
  • `route1[[i,]]` should be `route1[i,]` – Erin Dec 07 '18 at 02:12
  • I don't see it. – vincent Dec 07 '18 at 02:24
  • @Erin I see it. But now I have new error. – vincent Dec 07 '18 at 02:27
  • Yeah, it would be better to do a tidy version of this. With for-loops and indices, it's really easy to make little mistakes. – Erin Dec 07 '18 at 02:30
  • @Erin I did try other script; at the end I felt loop is the best solution. `route1` is extracted from a list, it contains more data frames with different dimensions. – vincent Dec 07 '18 at 02:33
  • Oh, I think I see what you're saying now. There are different numbers of columns, and you're doing this computation across the row. Is that correct? – Erin Dec 07 '18 at 02:35
  • @Erin Exactly.. – vincent Dec 07 '18 at 02:38

1 Answers1

1
# 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(.))
Erin
  • 386
  • 1
  • 7
  • I appreciate your help. However there is an error for the last pipe `Error in stop[1] : object of type 'closure' is not subsettable` – vincent Dec 07 '18 at 03:21
  • yep. typo. sorry, just a sec – Erin Dec 07 '18 at 03:27
  • I used `stop` instead of `stops` at some point. lol. might work now – Erin Dec 07 '18 at 03:31
  • It works now. Thank you so much! But I noticed all routes have the same time 12523. Is it supposed to be? None the less, thank you again. – vincent Dec 07 '18 at 03:34
  • Well, the grouping is fine for me, it doesn't just return the same thing. I think you're on your own from here since I don't have `mapdist`. ;) Good luck! – Erin Dec 07 '18 at 03:43
  • Hey Erin, this project is done with your help. I wrote a blog post for this object. https://vincent507.wordpress.com/2018/12/09/practice-what-ive-learned-a-plan-for-trip-of-san-diego/ – vincent Dec 09 '18 at 21:04
  • Aww, that's so, so sweet! Thank you for the credit! :D – Erin Dec 19 '18 at 10:41