# introduction

#### 2019-02-03

trread makes it easy to work with transit data by simplifying General Transit Feed Specification data (the standard format for storing transit data) into dataframes. Use it to calculate transit frequencies, and validate transit feeds.

trread is a fork of gtfsr, published to CRAN, with frequency calculation functions, and without GTFS-specific interactive cartography features.

## Installation

This package requires a working installation of sf.

    install.packages('trread')

    # For the development version from Github:
    # install.packages("devtools")
    devtools::install_github("r-transit/trread")

## Usage

### Read GTFS data

    # 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 = "trread")
    nyc <- read_gtfs(local_gtfs_path,
                     local=TRUE,
                     frequency=TRUE)

    ## Calculating route and stop headways.

### Headways by Route

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`.

    head(nyc$routes_frequency_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.15         76
    ## 2 2                      7            51          135.          120
    ## 3 3                      8             8            0.08         68
    ## 4 4                      6           115          205.           77
    ## 5 5                      9           110          271.          102
    ## 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

### Headways by Stop

View the headways at stops. `stops_frequency_df` is added to the list of gtfs dataframes read in by `read_gtfs`.

    head(nyc$stops_frequency_df)

    ## # A tibble: 6 x 6
    ##   route_id direction_id stop_id service_id               departures headway
    ##   <chr>           <int> <chr>   <chr>                         <int>   <dbl>
    ## 1 1                   0 101N    ASP18GEN-1087-Weekday-00        177    5.42
    ## 2 1                   0 103N    ASP18GEN-1087-Weekday-00        177    5.42
    ## 3 1                   0 104N    ASP18GEN-1087-Weekday-00        177    5.42
    ## 4 1                   0 106N    ASP18GEN-1087-Weekday-00        178    5.39
    ## 5 1                   0 107N    ASP18GEN-1087-Weekday-00        183    5.25
    ## 6 1                   0 108N    ASP18GEN-1087-Weekday-00        183    5.25

    ## # 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