You can use the functions dplyr::lag()
and base::diff()
.
The function diff(x, n)
will return the difference of the values of n
days ago.
The function lag(x, n)
will return the values of n
days ago.
So with the code below we have a weekly growth rate.
library(tidyverse)
dados <- read_delim("~/Downloads/arquivo_geral.csv",
";", escape_double = FALSE, trim_ws = TRUE)
dados %>%
group_by(regiao, estado) %>%
mutate(dif_semanal = c(rep(NA, 7), diff(casosAcumulados, 7)),
percentual_dif = dif_semanal / lag(casosAcumulados, 7)) %>%
select(-ends_with("os")) %>%
tail()
#> # A tibble: 6 x 5
#> # Groups: regiao, estado [1]
#> regiao estado data dif_semanal percentual_dif
#> <chr> <chr> <date> <dbl> <dbl>
#> 1 Centro-Oeste DF 2020-04-18 183 0.316
#> 2 Centro-Oeste DF 2020-04-19 213 0.347
#> 3 Centro-Oeste DF 2020-04-20 234 0.367
#> 4 Centro-Oeste DF 2020-04-21 230 0.353
#> 5 Centro-Oeste DF 2020-04-22 264 0.387
#> 6 Centro-Oeste DF 2020-04-23 247 0.345
Created on 2020-04-24 by the reprex package (v0.3.0)
Thanks for the solution! It worked perfectly.
– Alexandre Sanches