We’ll use NYC MTA subway data to demonstrate headway calculations, pulling directly from their URL.
A copy of these data are cached in the package for convenience and testing.
local_gtfs_path <- system.file("extdata", "google_transit_nyc_subway.zip", package = "tidytransit")
NYC <- import_gtfs(local_gtfs_path, local=TRUE)If you want to pull the file directly from the url you can also use:
List the routes with the shortest median headways.
route_frequency_summary <- route_frequency(NYC) %>%
  arrange(median_headways)
fast_routes <- filter(route_frequency_summary, median_headways<25)
knitr::kable(head(fast_routes))| 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 | 
List the stops with the shortest headways in the system.
stop_frequency_summary <- stop_frequency(NYC, by_route=FALSE) %>%
  inner_join(NYC$stops_df, by="stop_id") %>%
    select(stop_name, direction_id, stop_id, headway) %>%
      arrange(headway)
head(stop_frequency_summary)## # A tibble: 6 x 4
## # Groups:   direction_id, stop_id [6]
##   stop_name             direction_id stop_id headway
##   <chr>                        <int> <chr>     <dbl>
## 1 Times Sq - 42 St                 0 902N       3.60
## 2 Grand Central - 42 St            1 901S       3.60
## 3 Times Sq - 42 St                 1 902S       3.60
## 4 Grand Central - 42 St            0 901N       3.61
## 5 Mets - Willets Point             0 702N       3.72
## 6 Junction Blvd                    0 707N       3.72We can add map data to the feed by joining the shapes together around routes.
Lets see whats in the feed.
##  [1] "agency_df"         "calendar_dates_df" "calendar_df"      
##  [4] "routes_df"         "shapes_df"         "stop_times_df"    
##  [7] "stops_df"          "transfers_df"      "trips_df"         
## [10] "validation"Now lets turn the routes and stops tables in simple features data frames:
These are the routes and stops tables with simple features/geometries.
##  [1] "agency_df"         "calendar_dates_df" "calendar_df"      
##  [4] "routes_df"         "shapes_df"         "stop_times_df"    
##  [7] "stops_df"          "transfers_df"      "trips_df"         
## [10] "validation"        "sf_stops"          "sf_routes"Note that now there are two new tables in the data:
Now we can join these frequencies to route geometries and plot them with base R.
routes_headways_sf <- right_join(NYC$sf_routes,fast_routes, by="route_id")
routes_headways_sf_vars_only <- select(routes_headways_sf,-route_id)
plot(routes_headways_sf_vars_only)