I've seen a solution to this, but can't get it to work for groups (Fill NA in a time series only to a limited number), and thought there must be a neater way to do this also?
Say I have the following dt:
dt <- data.table(ID = c(rep("A", 10), rep("B", 10)), Price = c(seq(1, 10, 1), seq(11, 20, 1)))
dt[c(1:2, 5:10), 2] <- NA
dt[c(11:13, 15:19) ,2] <- NA
dt
ID Price
1: A NA
2: A NA
3: A 3
4: A 4
5: A NA
6: A NA
7: A NA
8: A NA
9: A NA
10: A NA
11: B NA
12: B NA
13: B NA
14: B 14
15: B NA
16: B NA
17: B NA
18: B NA
19: B NA
20: B 20
What I would like to do, is to fill NA
s both forward and back from the most recent non-NA
value, but only up to a maximum of two rows forward or back.
I also need it to be done by group (ID).
I have tried using na.locf
/na.approx
with maxgap = x
etc, but it does not fill NA
s where the gap between non-NA
values is greater than maxgap
. Whereas I want to fill these forward and back even if the gap between non-NA
values is greater than maxgap
, but only by two rows.
The final result should looks something like:
ID Price Price_Fill
1: A NA 3
2: A NA 3
3: A 3 3
4: A 4 4
5: A NA 4
6: A NA 4
7: A NA NA
8: A NA NA
9: A NA NA
10: A NA NA
11: B NA NA
12: B NA 14
13: B NA 14
14: B 14 14
15: B NA 14
16: B NA 14
17: B NA NA
18: B NA 20
19: B NA 20
20: B 20 20
In reality, my data set is massive, and I want to be able to fill NA
s forward and back for up to 672 rows, but no more, by group.
Thanks!