Skip to content

Instantly share code, notes, and snippets.

@polettif
Last active November 26, 2021 11:19
Show Gist options
  • Save polettif/6bd30c85193981be2e16d433d031f319 to your computer and use it in GitHub Desktop.
Save polettif/6bd30c85193981be2e16d433d031f319 to your computer and use it in GitHub Desktop.
trips during clock change (dst -> standard time) in Swiss GTFS
library(tidytransit)
library(dplyr)
library(tidytable)
# https://opentransportdata.swiss/de/dataset/timetable-2021-gtfs2020/resource/10ed3c73-231e-49ba-beac-2df54de48c29
g = read_gtfs("gtfs_fp2021_2021-11-24_09-10.zip")
summary(g)
#> tidygtfs object
#> files agency, stops, routes, trips, stop_times, calendar, calendar_dates, transfers, feed_info
#> agencies Schweizerische Bundesbahnen SBB, Sihltal-Zürich-Uetliberg-Bahn, BLS AG (bls) ... 459 more
#> service from 2020-12-13 to 2021-12-11
#> uses stop_times (no frequencies)
#> # routes 4834
#> # trips 1287857
#> # stop_ids 39183
#> # stop_names 33847
#> # shapes 0
get_trip_times = function(stop_times) {
stop_times %>%
summarise.(trip_start_time = min(departure_time),
trip_end_time = max(arrival_time), .by = trip_id) %>%
mutate.(trip_start_hour = floor(as.numeric(trip_start_time)/3600),
trip_end_hour = floor(as.numeric(trip_end_time)/3600))
}
# day before changing from dst to standard time
g1 = filter_feed_by_date(g, "2021-10-30")
# day of change
g2 = filter_feed_by_date(g, "2021-10-31")
trip_times1 = get_trip_times(g1$stop_times) %>% mutate(service_date = "2021-10-30")
trip_times2 = get_trip_times(g2$stop_times) %>% mutate(service_date = "2021-10-31")
trip_times = bind_rows(trip_times1, trip_times2) %>%
count.(service_date, trip_start_hour, trip_end_hour, name = "n_trips")
# Number of trips happening on both service days
sum(trip_times$n_trips)
#> [1] 320245
# Find trips between 00:00 and 00:30 on 2021-10-31
trips_between_0_and_3am = trip_times %>%
filter((service_date == "2021-10-30" & trip_end_hour >= 24) |
(service_date == "2021-10-31" & trip_start_hour < 3))
trips_between_0_and_3am %>%
summarise.(n_trips = sum(n_trips), .by = service_date)
#> # A tidytable: 2 × 2
#> service_date n_trips
#> <chr> <int>
#> 1 2021-10-30 5922
#> 2 2021-10-31 283
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment