Backward replacement of NAs in time series only to a limited number of observations

时光总嘲笑我的痴心妄想 提交于 2020-01-22 02:16:13

问题


In a data table I want to perform a forward and backward gap-filling procedure over a period of 3 days in both directions.

# Example data:
library(data.table)
library(zoo)
dt <- data.table(Value = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.1359223, NA, NA, NA, NA, 0.0000000, 0.0000000, 0.0000000, 0.0000000, 0.0000000, NA))

> dt
           Value 
1:          NA  
2:          NA  
3:          NA  
4:          NA  
5:          NA  
6:          NA  
7:          NA  
8:          NA  
9:          NA  
10:  0.1359223  
11:         NA  
12:         NA  
13:         NA  
14:         NA  
15:  0.0000000  
16:  0.0000000  
17:  0.0000000  
18:  0.0000000  
19:  0.0000000  
20:         NA  

Therefore I want to create two new columns, one with forward replacement of the NAs and one with the backward replacement.

# desired output
         Value      forward    backward
1:         NA           NA        NA
2:         NA           NA        NA
3:         NA           NA        NA
4:         NA           NA        NA
5:         NA           NA        NA
6:         NA           NA        NA
7:         NA           NA     0.1359223
8:         NA           NA     0.1359223
9:         NA           NA     0.1359223
10: 0.1359223      0.1359223   0.1359223
11:        NA      0.1359223      NA
12:        NA      0.1359223   0.0000000
13:        NA      0.1359223   0.0000000
14:        NA           NA     0.0000000    
15: 0.0000000      0.0000000   0.0000000
16: 0.0000000      0.0000000   0.0000000
17: 0.0000000      0.0000000   0.0000000
18: 0.0000000      0.0000000   0.0000000
19: 0.0000000      0.0000000   0.0000000
20:        NA      0.0000000          NA

The forward replacement works fine with the following code:

dt$forward <- NA
r <- rle(is.na(dt$Value))
dt$forward <- na.locf(dt$Value, fromLast = F, na.rm = F)
is.na(dt$forward) <- sequence(r$lengths) > 3 & rep(r$values, r$lengths)

But I don´t know how to modify that code to do the backward replacement. How can I get around this? Thank you!


回答1:


Hacky, but why not just flip your column?

Code

# Using your result as basis
dt$Value <- rev(dt$Value)                  
dt$backward <- NA
r <- rle(is.na(dt$Value))
dt$backward <- na.locf(dt$Value, fromLast = F, na.rm = F)
is.na(dt$backward) <- sequence(r$lengths) > 3 & rep(r$values, r$lengths)
dt$Value <- rev(dt$Value)                 
dt$backward <- rev(dt$backward)            

Result

> dt
        Value   forward  backward
 1:        NA        NA        NA
 2:        NA        NA        NA
 3:        NA        NA        NA
 4:        NA        NA        NA
 5:        NA        NA        NA
 6:        NA        NA        NA
 7:        NA        NA 0.1359223
 8:        NA        NA 0.1359223
 9:        NA        NA 0.1359223
10: 0.1359223 0.1359223 0.1359223
11:        NA 0.1359223        NA
12:        NA 0.1359223 0.0000000
13:        NA 0.1359223 0.0000000
14:        NA        NA 0.0000000
15: 0.0000000 0.0000000 0.0000000
16: 0.0000000 0.0000000 0.0000000
17: 0.0000000 0.0000000 0.0000000
18: 0.0000000 0.0000000 0.0000000
19: 0.0000000 0.0000000 0.0000000
20:        NA 0.0000000        NA


来源:https://stackoverflow.com/questions/53231287/backward-replacement-of-nas-in-time-series-only-to-a-limited-number-of-observati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!