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.
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# 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)
## Unzipped the following files to directory '/var/folders/hk/12t7fl6d08s7r494zd9sb8v40000gn/T//RtmpSzyzHv'...
## [1] "agency.txt" "calendar.txt" "calendar_dates.txt"
## [4] "routes.txt" "shapes.txt" "stop_times.txt"
## [7] "stops.txt" "transfers.txt" "trips.txt"
## Reading agency_df
## Reading calendar_df
## Reading calendar_dates_df
## Reading routes_df
## Reading shapes_df
## Reading stop_times_df
## Reading stops_df
## Reading transfers_df
## Reading trips_df
## ...done.
## Testing data structure...
## ...passed. Valid GTFS object.
## Calculating route and stop headways using defaults (6 am to 10 pm for weekday service).
## Converting stops to simple features
## Converting routes to simple features
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.
Filter to routes with shortest median headways
## # 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 |
You can also identify shortest headways by stop.
## # 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
Perhaps you want to map subway routes and color-code each route by how often trains come.
Source: Wikimedia, user -stk.