Below is an example, where I calculate travel time between to points. I create a tibble with start time
, duration
, end_time
and time_diff = end_time - start_time
. I wrote the tibble using write_csv()
and read it again with read_csv()
library(sf)
library(tidygeocoder)
library(osrm)
library(lubridate)
# 1. One World Trade Center, NYC
# 2. Madison Square Park, NYC
adresses <- c("285 Fulton St, New York, NY 10007",
"11 Madison Ave, New York, NY 10010")
# geocode the two addresses & transform to {sf} data structure
data <- tidygeocoder::geo(adresses, method = "osm") %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
rownames(data) <- c("One World Trafe Center", "Madison Square Park")
# calculate travel time from "One World Trade Center" to "Madison Square Park"
osroute <- osrmTable(src = data["One World Trade Center", ],
dst = data["Madison Square Park", ])
tbl_out <- tibble(start_trip = ymd_hms("2018-01-01 08:00:00", tz = "America/New_York"),
duration = osroute$durations,
end_trip = start_trip + 60 * osroute$durations,
time_diff = difftime(end_trip, start_trip, units = "mins")
write.csv(tbl_out,
"sample.csv")
tbl_in <- read_csv("sample.csv")
Here is the screenshot of tbl_out
(used to write the data)
Here is the screenshot of tbl_in
(read using read_csv)
Can someone help me fix some issues -
- Datetime changed from EST to UTC after writing into csv and then reading from csv file
- Time_diff and duration have same values. But time_diff is better readable and how can I add unit
mins
to duration. I don't want to convert it character and paste min. - Is there a way to convert time duration and time_diff to HH:MM and still perform basic operation like addition
- Follow up on 3. In case we convert time to HH:MM. Can I write 74.5 mins as 74:30.
time_diff
intbl_in
after reading from csv file does not have min