# Use of railtrails

#### Joshua Rosenberg

#### 2018-06-09

# railtrails

This R data package provides rail information about rail trails from the excellent TrailLink website, sponsored by the Rails-to-Trails Conservancy.

## Installation

You can install railtrails with the `install.packages()` function.

    install.packages("railtrails")

## Loading the data

Here is how to load the data:

    railtrails <- railtrails::railtrails
    railtrails

    ## # A tibble: 3,846 x 11
    ##    state name  distance surface category mean_review description n_reviews
    ##    <chr> <chr>    <dbl> <chr>   <chr>          <int> <chr>           <int>
    ##  1 AK    Chas…     14   Dirt, … Rail-Tr…           4 "Though cl…         1
    ##  2 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  3 AK    Bird…     13   Asphalt Rail-Tr…           5 "The Bird …         3
    ##  4 AK    Camp…      7.5 Asphal… Greenwa…           5 "The Campb…         3
    ##  5 AK    Goos…      1.5 Asphal… Greenwa…           0 "The sceni…         0
    ##  6 AK    Home…      4   Asphalt Greenwa…           5 "On the so…         1
    ##  7 AK    Lani…      3.9 Asphal… Greenwa…           3 "The Lanie…         1
    ##  8 AK    Palm…      6.1 Gravel  Rail-Tr…           0 "As its na…         0
    ##  9 AK    Ship…      2.6 Asphalt Rail-Tr…           4 "Ship Cree…         1
    ## 10 AL    Chie…     33   Asphalt Rail-Tr…           5 "In northe…        77
    ## # ... with 3,836 more rows, and 3 more variables: raw_reviews <list>,
    ## #   lat <dbl>, lng <dbl>

The latitude and longitude are for the trailhead and are from Google Maps searches of the trail’s name (and trail’s state). Around 94% of the trails are associated with latitude and longitude information.

See more information about the variables with `?railtrails`.

## “Unnesting” trail reviews

You may want to “unnest” the list-column with reviews in the following way:

    library(tidyr)
    d <- railtrails::railtrails
    d <- d %>% unnest(raw_reviews)
    d

    ## # A tibble: 24,413 x 11
    ##    state name  distance surface category mean_review description n_reviews
    ##    <chr> <chr>    <dbl> <chr>   <chr>          <int> <chr>           <int>
    ##  1 AK    Chas…     14   Dirt, … Rail-Tr…           4 "Though cl…         1
    ##  2 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  3 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  4 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  5 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  6 AK    Tony…     11   Asphalt Rail-Tr…           5 "The Tony …         5
    ##  7 AK    Bird…     13   Asphalt Rail-Tr…           5 "The Bird …         3
    ##  8 AK    Bird…     13   Asphalt Rail-Tr…           5 "The Bird …         3
    ##  9 AK    Bird…     13   Asphalt Rail-Tr…           5 "The Bird …         3
    ## 10 AK    Camp…      7.5 Asphal… Greenwa…           5 "The Campb…         3
    ## # ... with 24,403 more rows, and 3 more variables: lat <dbl>, lng <dbl>,
    ## #   raw_reviews <int>

## Processing category and surface (not run)

    library(dplyr)
    library(forcats)
    library(stringr)

    d <- d %>%
        mutate(category = as.factor(category),
               category = fct_recode(category, "Greenway/Non-RT" = "Canal"),
               mean_review = ifelse(mean_review == 0, NA, mean_review))

    d <- mutate(d,
                 surface_rc = case_when(
                     surface == "Asphalt" ~ "Paved",
                     surface == "Asphalt, Concrete" ~ "Paved",
                     surface == "Concrete" ~ "Paved",
                     surface == "Asphalt, Boardwalk" ~ "Paved",
                     str_detect(surface, "Stone") ~ "Crushed Stone",
                     str_detect(surface, "Ballast") ~ "Crushed Stone",
                     str_detect(surface, "Gravel") ~ "Crushed Stone",
                     TRUE ~ "Other"
                 )
    )

    d$surface_rc <- as.factor(d$surface_rc)

    d$surface_rc <- fct_relevel(d$surface_rc,
                                 "Crushed Stone")

    d

## Note

If you like using this package, please consider visiting or even making a donation to the Rails to Trails Conservancy at via https://www.traillink.com/.

Note that these data were last updated 2017-08-14 and so new trails and particularly reviews have likely been added since then. The `R` files at here can be used to create updated data. Pending the same permissions as indicated by the `robots.txt` file and terms of service for use of the TrailLink website, I will plan to update this package bi-yearly (this was last updated around 2018-02-2).

Thank you to Bob Rudis for feedback on an earlier version of this package.

## Future improvements

I am interested in adding the trailhead location to the data; this can be done fairly easily using the Google Maps API but will take considerable time due to the number of trails. Contributions are welcome. Requests for features can be made on GitHub.