Home > Back-end >  R - Count the number of reports a month before a week
R - Count the number of reports a month before a week

Time:01-11

This is very similar to the question I asked previously (see Count the number of rows a month before a date), but the solution suggested does not fix my issue in this case.

I have a dataframe that looks like this:

> Reports_per_park_per_week_3

Park               Week       Coy_Season Reports_per_week    Number_4w_AC Year 
  <chr>              <date>     <chr>                 <dbl>         <int> <chr>
1 Airways Park       2018-04-29 1                         5           0    2018 
2 Airways Park       2018-05-06 2                         2           1    2018 
3 Airways Park       2018-05-13 2                         0           1    2018 
4 Baker Park         2018-05-20 2                         3           2    2018 
5 Baker Park         2018-05-27 2                         9           2    2018 
6 Baker Park         2018-06-03 2                         2           5    2018 

I would like to create another column that would calculate the total number of reports per park in the month prior to the week being evaluated. The column in question would therefore have to take into account the Park column, the Week column and the Reports per week column.

> Reports_per_park_per_week_3

Park               Week       Coy_Season Reports_per_week    Number_4w_AC Year  Reports_4w
  <chr>              <date>     <chr>                 <dbl>         <int> <chr> 
1 Airways Park       2018-04-29 1                         5           0    2018          5
2 Airways Park       2018-05-06 2                         2           1    2018          7
3 Airways Park       2018-05-13 2                         0           1    2018          7
4 Baker Park         2018-05-20 2                         3           2    2018          3
5 Baker Park         2018-05-27 2                         9           2    2018         12
6 Baker Park         2018-06-03 2                         2           5    2018         14

CodePudding user response:

Does this do what you want? It is assumed here that your time series all have 1-week spacing throughout (no weeks are skipped) and that there are zero reports prior to the earliest week in each time series.

library("dplyr")
library("zoo")

Reports_per_park_per_week_3 %>% 
  group_by(Park) %>%
  arrange(Week, .by_group = TRUE) %>%
  mutate(Reports_4w = rollsum(c(integer(3L), Reports_per_week), 4L))
  •  Tags:  
  • Related