How to make inter-weekly variation on R

Asked

Viewed 45 times

2

I have a database in long format and I need to do the inter-weekly variation, ie today’s data / given 7 days ago - 1.

Code I used, but is doing the variation of the previous day:

library(readr)

dados <- read_delim("~/Downloads/arquivo_geral.csv", 
                                        ";", escape_double = FALSE, trim_ws = TRUE)

dados[["variacaoCasos"]] <- ((dados$casosAcumulados/lag(dados$casosAcumulados, k = 7))-1)

Database I’m using: https://covid.saude.gov.br/

Thank you.

1 answer

3


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.