-
Notifications
You must be signed in to change notification settings - Fork 0
/
2019_02_26_tidy_tuesday.Rmd
96 lines (58 loc) · 2.4 KB
/
2019_02_26_tidy_tuesday.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: "TidyTemplate"
date: 2021-01-30
output: html_output
editor_options:
chunk_output_type: console
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
theme_set(theme_light())
```
# Load the weekly Data
Download the weekly data and make available in the `tt` object.
```{r }
tt <- tidytuesdayR::tt_load('2019-02-26')
trains <- tt$full_trains %>%
mutate(date = lubridate::ymd(paste0(as.character(year),if_else(nchar(month) == 1, paste0("0",month),as.character(month)),"01")),
yearmonth = tsibble::yearmonth(date),
train_leg = paste0(departure_station,"-",arrival_station))
trains_key <- trains %>%
filter(!is.na(service)) %>%
select(train_leg, service) %>%
rename(service_update = service) %>%
distinct()
trains <- trains %>%
left_join(trains_key) %>%
mutate(service = case_when(
!is.na(service_update) ~ service_update,
str_detect(train_leg,"MADRID") ~ "International",
str_detect(train_leg, "BARCELONA") ~ "International",
T ~ "National")) %>%
select(-service_update)
late_stats <- . %>%
summarise(cancel_rate = sum(num_of_canceled_trains)/sum(total_num_trips),
late_rate = sum(num_late_at_departure)/sum(total_num_trips),
total_trips = sum(total_num_trips))
# tidyverts --------------------------------------------------------------
library(tsibble)
library(fabletools)
library(feasts)
trains_ts <- trains %>%
tsibble::tsibble(key = c(service, departure_station, arrival_station), index = yearmonth)
## Initial Viz
trains_ts %>%
autoplot(total_num_trips) +
guides(col = FALSE) +
labs(y = "Trips", x = NULL, title = "Monthly Total Trips by Train Leg")
## Feature Extraction
train_features <- trains_ts %>%
fabletools::features(total_num_trips, fabletools::feature_set(pkgs = "feasts"))