Reputation: 115
I have some time series daily data as such:
date cases
1 5/1/2022 26582
2 5/2/2022 14921
3 5/3/2022 38811
4 5/4/2022 20838
5 5/5/2022 19382
6 5/6/2022 18768
7 5/7/2022 49466
8 5/8/2022 40081
9 5/9/2022 19993
10 5/10/2022 49662
11 5/11/2022 45268
12 5/12/2022 38406
13 5/13/2022 36880
14 5/14/2022 39393
15 5/15/2022 30418
16 5/16/2022 15220
17 5/17/2022 43684
18 5/18/2022 42053
19 5/19/2022 36577
20 5/20/2022 34825
21 5/21/2022 35802
22 5/22/2022 26678
23 5/23/2022 12743
24 5/24/2022 37721
25 5/25/2022 34616
26 5/26/2022 28079
27 5/27/2022 24891
28 5/28/2022 25026
29 5/29/2022 17369
30 5/30/2022 8367
31 5/31/2022 24818
32 6/1/2022 22847
33 6/2/2022 19334
34 6/3/2022 17917
35 6/4/2022 17927
36 6/5/2022 13350
37 6/6/2022 6589
38 6/7/2022 20085
39 6/8/2022 18234
40 6/9/2022 15891
41 6/10/2022 14707
42 6/11/2022 15422
43 6/12/2022 11845
44 6/13/2022 6001
45 6/14/2022 17742
46 6/15/2022 16497
47 6/16/2022 14833
48 6/17/2022 13838
49 6/18/2022 15260
50 6/19/2022 11630
51 6/20/2022 5720
52 6/21/2022 18223
53 6/22/2022 17182
54 6/23/2022 16043
55 6/24/2022 14879
56 6/25/2022 16492
57 6/26/2022 12995
58 6/27/2022 7140
59 6/28/2022 22927
60 6/29/2022 23376
61 6/30/2022 22515
62 7/1/2022 24113
63 7/2/2022 27026
64 7/3/2022 21813
65 7/4/2022 13364
66 7/5/2022 44746
67 7/6/2022 47535
68 7/7/2022 48275
69 7/8/2022 49235
70 7/9/2022 59011
71 7/10/2022 50908
72 7/11/2022 27452
73 7/12/2022 97771
74 7/13/2022 102621
75 7/14/2022 98980
76 7/15/2022 99763
77 7/16/2022 117180
78 7/17/2022 99985
79 7/18/2022 76164
80 7/19/2022 66725
81 7/20/2022 152495
82 7/21/2022 186209
83 7/22/2022 195136
84 7/23/2022 200937
85 7/24/2022 176517
86 7/25/2022 126535
87 7/26/2022 196453
88 7/27/2022 209657
89 7/28/2022 233066
90 7/29/2022 221409
91 7/30/2022 222274
92 7/31/2022 197750
93 8/1/2022 139668
I require to find the rolling week-on-week % change in cases, which is basically the % change in cases over the last 7 days relative to the previous 7 days. I know how to implement this on Excel but unsure how to do this on R. To illustrate what I require:
% change on 5/14/2022 = ([(sum of cases over from row 14 to 8) / (sum of cases from row 7 to 1)] - 1)*100
then % change on 5/15/2022 = ([(sum of cases over from row 15 to 9) / (sum of cases from row 8 to 2)] - 1)*100 and so on...
Any help will be greatly appreciated.
Upvotes: 0
Views: 72
Reputation: 269481
Define function chg as the percent change given 14 days of data and use rollapplyr to apply it in a rolling fashion.
library(zoo)
chg <- function(x) 100 * sum(tail(x, 7)) / sum(head(x, 7)) - 100
transform(DF, chg = rollapplyr(cases, 14, chg, fill = NA))
First 20 rows.
Lines <- " date cases
1 5/1/2022 26582
2 5/2/2022 14921
3 5/3/2022 38811
4 5/4/2022 20838
5 5/5/2022 19382
6 5/6/2022 18768
7 5/7/2022 49466
8 5/8/2022 40081
9 5/9/2022 19993
10 5/10/2022 49662
11 5/11/2022 45268
12 5/12/2022 38406
13 5/13/2022 36880
14 5/14/2022 39393
15 5/15/2022 30418
16 5/16/2022 15220
17 5/17/2022 43684
18 5/18/2022 42053
19 5/19/2022 36577
20 5/20/2022 34825"
DF <- read.table(text = Lines)
Upvotes: 1