Learn R Programming

gtfstools (version 1.4.0)

convert_time_to_seconds: Convert time fields to seconds after midnight

Description

Converts stop_times' and frequencies' fields in the "HH:MM:SS" format to seconds after midnight. Instead of overwritting the existing fields, creates new fields with the _secs suffix.

Usage

convert_time_to_seconds(gtfs, file = NULL, by_reference = FALSE)

Value

If by_reference is FALSE, returns a GTFS object with additional time in seconds columns (identified by a _secs suffix). Else, returns a GTFS object invisibly (please note that in such case the original GTFS object is altered).

Arguments

gtfs

A GTFS object, as created by read_gtfs().

file

A character vector, specifying the file whose fields should be converted. If NULL (the default), the function attempts to convert the times from both files, but only raises an error if none of them exist.

by_reference

Whether to update the tables by reference. Defaults to FALSE.

Examples

Run this code
# \dontshow{
  old_dt_threads <- data.table::setDTthreads(1)
  on.exit(data.table::setDTthreads(old_dt_threads), add = TRUE)
# }
data_path <- system.file("extdata/spo_gtfs.zip", package = "gtfstools")

gtfs <- read_gtfs(data_path)

# by default converts both 'stop_times' and 'frequencies' times
converted_gtfs <- convert_time_to_seconds(gtfs)
head(converted_gtfs$stop_times)
head(converted_gtfs$frequencies)

# choose which table to convert with 'file'
converted_gtfs <- convert_time_to_seconds(gtfs, file = "frequencies")
head(converted_gtfs$stop_times)
head(converted_gtfs$frequencies)

# original gtfs remained unchanged, as seen with the frequencies table above
# change original object without creating a copy with 'by_reference = TRUE'
convert_time_to_seconds(gtfs, by_reference = TRUE)
head(gtfs$stop_times)
head(gtfs$frequencies)

Run the code above in your browser using DataLab