Last active
May 18, 2020 19:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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