tidytransit makes it easy to work with transit data by simplifying General Transit Feed Specification data (the standard format for storing transit data) into tidyverse and sf-friendly dataframes. Use it to map existing stops and routes, calculate transit frequencies, and validate transit feeds.
tidytransit is a fork of gtfsr, published to CRAN, with frequency calculation functions, and without GTFS-specific interactive cartography features.
This package requires a working installation of sf.
# Once sf is installed, you can install from CRAN with:
install.packages('tidytransit')
# For the development version from Github:
# install.packages("devtools")
devtools::install_github("r-transit/tidytransit")
For some users, sf
is impractical to install due to system level dependencies. For these users, trread
may work better. It has more limited functionality, but it can read GTFS tables into R.
# Read in GTFS feed
# here we use a feed included in the package, but note that you can read directly from the New York City Metropolitan Transit Authority using the following URL:
# nyc <- read_gtfs("http://web.mta.info/developers/data/nyct/subway/google_transit.zip")
local_gtfs_path <- system.file("extdata",
"google_transit_nyc_subway.zip",
package = "tidytransit")
nyc <- read_gtfs(local_gtfs_path,
local=TRUE,
geometry=TRUE,
frequency=TRUE)
## Calculating route and stop headways using defaults (6 am to 10 pm for weekday service).
Tidytransit attempts to convert GTFS feeds into simple features dataframes and frequency/headway dataframes upon import of the GTFS data.
Below we discuss methods and data available for the gtfs object as read by tidytransit.
Perhaps you want to map subway routes and color-code each route by how often trains come.
View the headways along routes as a dataframe. routes_frequency_df
is added to the list of gtfs dataframes read in by read_gtfs
.
Use the GTFS Table Relationships diagram below to join these data to relevant data, for example, the more detailed route names on the routes_df
.
## # A tibble: 6 x 5
## route_id median_headways mean_headways st_dev_headways stop_count
## <chr> <int> <int> <dbl> <int>
## 1 1 5 5 0.14 76
## 2 2 8 36 63.7 118
## 3 3 8 8 0.06 68
## 4 4 7 197 350. 75
## 5 5 10 97 245. 100
## 6 5X 48 48 0 29
route_id | median_headways | mean_headways | st_dev_headways | stop_count |
---|---|---|---|---|
GS | 4 | 4 | 0.01 | 4 |
L | 4 | 4 | 0.13 | 48 |
1 | 5 | 5 | 0.14 | 76 |
7 | 5 | 5 | 0.29 | 44 |
6 | 6 | 7 | 2.84 | 76 |
E | 6 | 23 | 53.01 | 48 |
View the headways at stops. stops_frequency_df
is added to the list of gtfs dataframes read in by read_gtfs
.
## # A tibble: 6 x 6
## # Groups: route_id, direction_id, stop_id [6]
## route_id direction_id stop_id service_id departures headway
## <chr> <int> <chr> <chr> <int> <dbl>
## 1 1 0 101N ASP18GEN-1087-Weekday-… 173 5.55
## 2 1 0 103N ASP18GEN-1087-Weekday-… 173 5.55
## 3 1 0 104N ASP18GEN-1087-Weekday-… 173 5.55
## 4 1 0 106N ASP18GEN-1087-Weekday-… 174 5.52
## 5 1 0 107N ASP18GEN-1087-Weekday-… 179 5.36
## 6 1 0 108N ASP18GEN-1087-Weekday-… 178 5.39
## # A tibble: 6 x 4
## # Groups: direction_id, stop_id [6]
## direction_id stop_id stop_name headway
## <int> <chr> <chr> <dbl>
## 1 0 902N Times Sq - 42 St 3.60
## 2 1 901S Grand Central - 42 St 3.60
## 3 1 902S Times Sq - 42 St 3.60
## 4 0 901N Grand Central - 42 St 3.61
## 5 0 702N Mets - Willets Point 3.72
## 6 0 707N Junction Blvd 3.72