Skip to content

Instantly share code, notes, and snippets.

@njdehoog
Last active October 14, 2020 15:35
Show Gist options
  • Save njdehoog/4c489d02769f69c9ed49f8f7630e859f to your computer and use it in GitHub Desktop.
Save njdehoog/4c489d02769f69c9ed49f8f7630e859f to your computer and use it in GitHub Desktop.
---
title: "BCU Covid-19 statistics"
output:
pdf_document: default
html_document: default
---
## Install dependencies
```{r}
install.packages("rvest")
library(rvest)
```
## Get the data
```{r}
html <- read_html("https://www.bcu.ac.uk/about-us/coronavirus-information/coronavirus-statistics")
html %>% rvest::html_node('table')
covid_stats <- html %>% rvest::html_node('table') %>%
html_table()
```
## Interpret dates
```{r}
covid_stats$Date <- as.Date(covid_stats$Date, format="%A %d %B")
```
## Add a totals column
```{r}
totals <- covid_stats$`Staff on-campus`+covid_stats$`Students on-campus`+covid_stats$`Students off-campus`
covid_stats <- cbind(covid_stats, Total=totals)
```
## Install Zoo
```{r}
install.packages("zoo")
```
## Add rolling averages
```{r}
library(zoo)
rolling_average <- rollmean(covid_stats$Total, 7, fill = list(NA, NULL, NA), align="left")
covid_stats$Rolling_average <- rolling_average
```
## Plot the data
```{r}
library(ggplot2)
plot <- ggplot(subset(covid_stats, !is.na(Rolling_average)), aes(Date,Rolling_average, group=1)) +
geom_line() + labs(x= "", y = "", title = "Daily confirmed cases at BCU, 7-day rolling average")
```
## Save plot to PNG file
```{r}
ggsave("plot.png", width = 8, height = 5, dpi = 300)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment