Commit 615c1bf2 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

Add comments and clean up calculation of Dfr3.

parent f941a4de
......@@ -147,8 +147,8 @@ Lock$name <- reorder(Lock$name, Lock$T80)
```{r country-data}
## Variables relative to European countries:
## group, name, lockdown date, population, cumulated deaths,
## Variables relative to African countries:
## group, name, lockdown date, population, cumulated deaths (as of today),
## and death rate.
country_data <-
map_dfr(
......@@ -189,6 +189,7 @@ country_data <-
## with population and lockdown date added
Dfr <- deaths_by_ctry_dt %>%
inner_join(
## filter African countries by joining
country_data,
by = "name"
) %>%
......@@ -196,8 +197,9 @@ Dfr <- deaths_by_ctry_dt %>%
# Order name levels in decreasing order of death rate
name = fct_reorder(name, death_rate, .desc = TRUE)
) %>%
select(name, date, deaths, pop, lockdown)
select(name, date, deaths, pop, lockdown, current_cum_deaths = cum_deaths)
## onam is equal to african_countries but ordered by decreasing death-rate
onam <- levels(Dfr$name)
## Names of countries with less than 25 cumulated deaths
......@@ -207,22 +209,20 @@ nam25 <- country_data %>%
onam2 <- onam[!(onam %in% nam25)]
## the condition on deaths >= 0 is needed because Spain has declared a
## negative count in early June
Dfr2 <- subset(Dfr, name %in% onam2 & deaths >= 0)
Dfr2$name <- factor(as.character(Dfr2$name),
levels = onam2)
Dfr3 <- do.call(
"rbind",
by(Dfr2,
list(name = Dfr2$name),
function(x){
x$day = as.numeric(x$date - min(x$date, na.rm = T))
x
}))
Dfr3$name <- factor(as.character(Dfr3$name),
levels = onam2)
## Compute day since first reported death by country, for countries with at
## least 25 cumulative deaths
Dfr3 <- Dfr %>%
## the condition on deaths >= 0 is needed because Spain has declared a
## negative count in early June
filter(current_cum_deaths >= 25, deaths >= 0) %>%
droplevels() %>%
group_by(name) %>%
mutate(
day = as.integer(date - min(date, na.rm = TRUE))
) %>%
ungroup() %>%
select(-current_cum_deaths)
## use function "by" to loop over the country names
L2 <- by(Dfr3,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment