Skip to content

Commit

Permalink
BRA
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele-guidotti committed Mar 1, 2021
1 parent 9c4b88d commit 7960bfb
Show file tree
Hide file tree
Showing 6 changed files with 5,812 additions and 726 deletions.
61 changes: 61 additions & 0 deletions R/ds_covid19br_git.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
covid19br_git <- function(cache, level){

if(level<=2){

# download
url <- "https://raw.githubusercontent.com/wcota/covid19br/master/cases-brazil-states.csv"
x <- read.csv(url, cache = cache)

# formatting
x <- map_data(x, c(
"date" = "date",
"state" = "state",
"deaths" = "deaths",
"totalCases" = "confirmed",
"recovered" = "recovered",
"tests" = "tests",
"vaccinated" = "vaccines_1",
"vaccinated_second" = "vaccines_2"
))

# total number of doses
x$vaccines <- x$vaccines_1 + x$vaccines_2

# filter
idx <- which(x$state=="TOTAL")
if(level==1)
x <- x[idx,]
else
x <- x[-idx,]

}
else {

# url
url <- "https://raw.githubusercontent.com/wcota/covid19br/master/cases-brazil-cities-time.csv.gz"

# download
tmp <- tempfile()
download.file(url, destfile=tmp, mode="wb", quiet = TRUE)
x <- read.csv(tmp, cache = cache)

# formatting
x <- map_data(x, c(
"date" = "date",
"ibgeID" = "code",
"state" = "state",
"deaths" = "deaths",
"totalCases" = "confirmed"
))

# filter cities
x <- x[nchar(x$code)==7,]

}

# date
x$date <- as.Date(x$date)

# return
return(x)
}
36 changes: 36 additions & 0 deletions R/ds_gov_br.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
gov_br <- function(cache, level){

if(level!=3)
return(NULL)

# url
url <- "https://s3-sa-east-1.amazonaws.com/ckan.saude.gov.br/PNI/vacina/2021/part-00000-090405dc-80c4-4889-84c3-a9a390d06947-c000.csv"

# download
tmp <- tempfile()
download.file(url, tmp)

# read
colClasses <- rep("NULL", 32)
colClasses[8] <- "character"
colClasses[28] <- "Date"
x <- read.csv(tmp, colClasses = colClasses)

# clean
file.remove(tmp)

# formatting
colnames(x) <- c("city", "date")

# cumulate
x <- x %>%
dplyr::filter(date > "2020-12-01") %>%
dplyr::group_by(city, date) %>%
dplyr::summarise(vaccines = dplyr::n()) %>%
dplyr::group_by(city) %>%
dplyr::arrange(date) %>%
dplyr::mutate(vaccines = cumsum(vaccines))

# return
return(x)
}
31 changes: 21 additions & 10 deletions R/iso_BRA.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
BRA <- function(level, cache) {

# fallback
if(level < 2)
return(NULL)
x <- covid19br_git(level = level, cache = cache)

# download (Espirito Santo)
x <- gov_br_es(level = level, cache = cache)
if(level==2)
x$id <- id(x$state, iso = "BRA", ds = "covid19br_git", level = level)

# id
if(level == 2)
x$id <- id(x$State, iso = "BRA", ds = "gov_br_es", level = level)
if(level == 3)
x$id <- id(x$Municipio, iso = "BRA", ds = "gov_br_es", level = level)
if(level==3){

# download Espirito Santo
y <- gov_br_es(level = level, cache = cache)
y$id <- id(y$Municipio, iso = "BRA", ds = "gov_br_es", level = level)

# vaccines
v <- gov_br(level = level, cache = cache)
v$id <- id(v$city, iso = "BRA", ds = "gov_br", level = level)

# merge
x$id <- id(x$code, iso = "BRA", ds = "covid19br_git", level = level)
x <- x %>%
dplyr::filter(state != "ES") %>%
dplyr::bind_rows(y) %>%
dplyr::left_join(v, by = c("id", "date"))

}

# return
return(x)

Expand Down
Loading

0 comments on commit 7960bfb

Please sign in to comment.