I have a table:
ID Dates Rates
1 2010-01-01 0
1 2010-01-02 0
1 2010-01-03 2
1 2010-01-04 2
1 2010-01-05 2
1 2010-01-06 1
1 2010-01-07 0
1 2010-01-08 0
1 2010-01-09 0
1 2010-01-10 0
2 2010-01-01 3
2 2010-01-02 3
2 2010-01-03 2
And I want to calculate the third column called "median rates" in Rstudio to show the median value of every 5 consecutive rows and the table should look as below
ID Dates Rates Median_Rates
1 2010-01-01 0 2
1 2010-01-02 0 2
1 2010-01-03 2 2
1 2010-01-04 2 2
1 2010-01-05 2 2
1 2010-01-06 1 0
1 2010-01-07 0 0
1 2010-01-08 0 0
1 2010-01-09 0 0
1 2010-01-10 0 0
2 2010-01-01 3 3
2 2010-01-02 3 3
2 2010-01-03 2 3
And then apply this to all IDs and more than 1 million rows in the dataset?
I want to calculate the median value of Rate
for every consecutive 5 rows (e.g. this position +/- 5 rows) by group (ID) and use that as the value of Median_Rates
.