knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This is practically the same code you can find on this blog post of mine:
https://www.brodrigues.co/blog/2018-11-14-luxairport/
but with some minor updates to reflect the current state of the {tidyverse}
packages as well
as logging using {loud}
.
library(loud) library(dplyr) library(tidyr) library(stringr) library(lubridate) data("avia") # Define required functions l_select <- loudly(select) l_pivot_longer <- loudly(pivot_longer) l_filter <- loudly(filter) l_mutate <- loudly(mutate) l_separate <- loudly(separate) l_group_by <- loudly(group_by) l_summarise <- loudly(summarise)
avia_clean <- avia %>% l_select(1, contains("20")) %>% # select the first column and every column starting with 10 bind_loudly(l_pivot_longer, -starts_with("unit"), names_to = "date", values_to = "passengers") %>% bind_loudly(l_separate, col = 1, into = c("unit", "tra_meas", "air_pr\\time"), sep = ",")
Let’s focus on monthly data:
avia_monthly <- avia_clean %>% bind_loudly(l_filter, tra_meas == "PAS_BRD_ARR", !is.na(passengers), str_detect(date, "M")) %>% bind_loudly(l_mutate, date = paste0(date, "01"), date = ymd(date)) %>% bind_loudly(l_select, destination = "air_pr\\time", date, passengers)
Now that the data is clean, we can take a look at the log and see what was done:
avia_monthly %>% pick("log")
This is especially useful if the object avia_monthly
gets saved using saveRDS()
. People that
then read this object, can read the log to know what happened and reproduce the steps if necessary.
Let's take a look at the final data set:
avia_monthly %>% pick("result")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.