Skip to content

Instantly share code, notes, and snippets.

@RandomCriticalAnalysis
Created March 22, 2020 19:10
Show Gist options
  • Save RandomCriticalAnalysis/7437564b3d7ac908e09defe259d66867 to your computer and use it in GitHub Desktop.
Save RandomCriticalAnalysis/7437564b3d7ac908e09defe259d66867 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(readxl)
library(httr)
library(countrycode)
library(janitor)
library(hrbrthemes)
library(ggrepel)
library(scales)
library(zoo)
#devtools::install_github("slowkow/ggrepel")
getPop=function() {
read_csv("projected-population-by-country.csv") %>%
clean_names() %>%
filter(
year == 2020,
!is.na(code)
) %>%
transmute(
location=code,
pop=population
)
}
getEuro=function() {
pop=getPop()
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
data <- read_excel(tf) %>%
clean_names() %>%
mutate(
countries_and_territories=str_replace_all(countries_and_territories,'_',' '),
location=countrycode(countries_and_territories,'country.name','iso3c'),
date_rep=as.Date(date_rep)
#location=countrycode(geo_id,'iso2c','iso3c')
) %>%
filter(
!is.na(location)
) %>%
arrange(location,date_rep) %>%
group_by(location) %>%
mutate(
cum_cases=cumsum(cases),
cum_deaths=cumsum(deaths)
) %>%
ungroup() %>%
rename(
country=countries_and_territories,
date=date_rep
) %>%
merge(pop,by='location',all.x=T) %>%
mutate(
cases_pc=cases/pop,
deaths_pc=deaths/pop,
cum_cases_pc=cum_cases/pop,
cum_deaths_pc=cum_deaths/pop
) %>%
group_by(location) %>%
mutate(
fd=min( ifelse(cum_deaths>0,date,as.Date(NA) ),na.rm=T ),
ld=max( ifelse(cum_deaths>0,date,as.Date(NA)),na.rm=T ),
fc=min( ifelse(cum_cases>0,date,as.Date(NA)),na.rm=T ),
lc=max( ifelse(cum_cases>0,date,as.Date(NA)),na.rm=T ),
deaths_pc_ma2=rollmeanr(deaths_pc,2,fill=NA),
deaths_pc_ma3=rollmeanr(deaths_pc,3,fill=NA),
deaths_pc_ma4=rollmeanr(deaths_pc,4,fill=NA),
deaths_pc_ma5=rollmeanr(deaths_pc,5,fill=NA),
deaths_pc_ma6=rollmeanr(deaths_pc,6,fill=NA),
deaths_pc_ma7=rollmeanr(deaths_pc,7,fill=NA)
) %>%
ungroup()
data
}
df=getEuro()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment