Skip to content

Instantly share code, notes, and snippets.

@nikdudaev
Last active May 18, 2020 19:16
Show Gist options
  • Save nikdudaev/436ecb19dba99e07d01d32d2936116c8 to your computer and use it in GitHub Desktop.
Save nikdudaev/436ecb19dba99e07d01d32d2936116c8 to your computer and use it in GitHub Desktop.
Our World in Data data transformation for this notebook https://observablehq.com/@nd/recreating-covid-19-cases-chart
library(readr)
library(dplyr)
library(tidyr)
library(caTools)
options(scipen = 999999)
df <- read_csv("~/Downloads/owid-covid-data.csv")
countries_to_use <- c(
"Austria",
"Czech Republic",
"France",
"Germany",
"Greece",
"Hungary",
"Netherlands",
"Poland",
"Russia",
"Slovakia",
"Slovenia",
"Sweden"
)
df <- df %>%
filter(location %in% countries_to_use) %>%
select(location, date, total_cases, new_cases, total_deaths, new_deaths) %>%
group_by(location) %>%
mutate(
days_since_100 = as.numeric(date - min(date[total_cases >= 100])),
days_since_5 = as.numeric(date - min(date[total_deaths >= 5]))
) %>%
ungroup()
cases <- df %>%
select(location, date, total_cases, new_cases, days_since_100) %>%
group_by(location) %>%
filter(days_since_100 >= 0) %>%
mutate(
rolling3 = runmean(new_cases, 3),
rolling7 = runmean(new_cases, 7),
rolling14 = runmean(new_cases, 14),
rolling30 = runmean(new_cases, 30)
) %>%
ungroup()
deaths <- df %>%
select(location, date, total_deaths, new_deaths, days_since_5) %>%
group_by(location) %>%
filter(days_since_5 >= 0) %>%
mutate(
rolling3 = runmean(new_deaths, 3),
rolling7 = runmean(new_deaths, 7),
rolling14 = runmean(new_deaths, 14),
rolling30 = runmean(new_deaths, 30)
) %>%
ungroup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment