Search code examples
rdatetimeimputation

Imputing values when NA <10 continuous NA


I am looking to replace NA in time series data with a moving average. I only want to impute if NA is continuous for <10 observations.

        TIME    HR
1     00:00:00  NA
2     00:01:00  NA
3     00:02:00  NA
4     00:03:00  NA
5     00:04:00  NA
6     00:05:00  55
7     00:06:00  56
8     00:07:00  NA
9     00:08:00  NA
10    00:09:00  58
11    00:10:00  58
12    00:11:00  NA
13    00:12:00  NA
14    00:13:00  NA
15    00:14:00  NA
16    00:15:00  NA
17    00:16:00  NA
18    00:17:00  NA
19    00:18:00  NA
20    00:19:00  NA
21    00:20:00  NA
22    00:21:00  NA
23    00:22:00  NA
24    00:23:00  NA
25    00:24:00  NA
26    00:25:00  NA
27    00:26:00  NA
28    00:27:00  NA
29    00:28:00  NA
30    00:29:00  NA
31    00:30:00  NA

Can anyone offer any advice? I can't seem for find a package that can help.

Thanks


Solution

  • You can use maxgap in zoo::na.locf

    library(zoo)
    na.locf(df$HR, maxgap = 10, fromLast = T)