Last active
December 13, 2021 23:21
-
-
Save fernandobarbalho/38478e7a4054242db2c442a20c584860 to your computer and use it in GitHub Desktop.
Function to create COVID-19 dataset from JHU github
This file contains hidden or 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
get_covid_data_jhu<- function(dt_ini, dt_fim, us_columns = TRUE, country = NULL ){ #data inicial para o download, data final para o download, indicação se trabalha com colunas dos EUA (TRUE por default) e lista de países (todos por default) | |
library(dplyr) | |
#cria um vetor de datas que vai ser iterado na formação do endereço de github que tem os dados | |
dates<- c(lubridate::ymd(dt_ini):lubridate::ymd(dt_fim)) | |
#O map_dfr vai montar dinamicamente um único dataframe com as para todas as datas do array | |
purrr::map_dfr(dates, function(a_date){ | |
urlfile<-paste0("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/",format(as.Date(a_date, origin= "1970-01-01"), "%m-%d-%Y"),".csv") | |
print(urlfile) | |
#Em cada iteração o objeto data_frame jhu_data é alimentado e no final do laço o map_dfr se encarrega de fazer a concatenação | |
jhu_data<- readr::read_csv(url(urlfile)) | |
if (!is.null(country)){ | |
jhu_data<- | |
jhu_data %>% | |
filter(Country_Region %in% country) | |
} | |
if (!us_columns){ | |
jhu_data<- | |
jhu_data %>% | |
select(-c(1:3)) | |
} | |
}) | |
} | |
#example | |
df_covid_jhu<- get_covid_data_jhu("2020-05-24","2020-05-25", us_columns = FALSE, country = c("US", "Brazil")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Obrigado João Vítor. Ficou ótimo o seu código!
Parabéns pela iniciativa