简体   繁体   English

在时间序列上向后替换NA的数量有限

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

In a data table I want to perform a forward and backward gap-filling procedure over a period of 3 days in both directions. 在数据表中,我想在两个方向上执行为期3天的向前和向后的间隙填充过程。

# 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. 因此,我想创建两列新列,一列向前替换NA,一列向后替换。

# 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! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅将时间序列中的 NA 填充到有限数量 - Fill NA in a time series only to a limited number 仅针对特定数量的日期在时间序列中填充 NA - Fill NAs in a time series for specific number of dates only 如何对时间序列中的缺失值进行插值,受限于连续NA(R)的数量? - How to interpolate missing values in a time series, limited by the number of sequential NAs (R)? 如何查找时间序列中的缺失观测值并填充NA - How to FIND missing observations within a time series and fill with NAs R时间序列-识别缺失的观测值(时间戳)并插入NA以创建给定长度的时间序列 - R timeseries - identify missing observations (timestamps) and insert NAs to create time series of given length R时间序列数据的闪亮图,ggplot,仅产生NA - R Shiny plot for time series data, ggplot, producing NAs only 如何用不相等的观测数汇总时间序列数据与R - How to summarise time-series data with unequal number of observations with R 根据时间序列中每年的观察次数进行总结 - Summarise based on number of observations per year in a time-series R-在时间序列中仅细分一个小时,并进行多个小时观测 - R - subsetting only one hour in time series with multiple hourly observations 如果时间序列包含 NA 且biasadj = TRUE,则 Auto.arima 仅返回拟合值的 NA - Auto.arima only returns NAs for fitted values if time series contains NAs and biasadj = TRUE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM