仅按时间序列填写NA到有限的数量

时间:2014-09-19 18:28:22

标签: r time-series xts zoo

我们是否可以在NAzoo对象中填充xtsNA s前进数量有限。换句话说,就像填充NA最多连续3个NA一样,然后将NA s从第4个值开始,直到有效数字为止。

像这样。

library(zoo)
x <- zoo(1:20, Sys.Date() + 1:20)
x[c(2:4, 6:10, 13:18)] <- NA
x

2014-09-20 2014-09-21 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-26 
         1         NA         NA         NA          5         NA         NA 
2014-09-27 2014-09-28 2014-09-29 2014-09-30 2014-10-01 2014-10-02 2014-10-03 
        NA         NA         NA         11         12         NA         NA 
2014-10-04 2014-10-05 2014-10-06 2014-10-07 2014-10-08 2014-10-09 
        NA         NA         NA         NA         19         20

所需的输出,将是变量n = 3的东西

2014-09-20 2014-09-21 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-26 
         1         1         1        1          5         5        5 
2014-09-27 2014-09-28 2014-09-29 2014-09-30 2014-10-01 2014-10-02 2014-10-03 
        5         NA         NA         11         12         12        12 
2014-10-04 2014-10-05 2014-10-06 2014-10-07 2014-10-08 2014-10-09 
        12         NA         NA         NA         19         20

我已经尝试了很多与na.locf(x, maxgap = 3)等的组合而没有取得多大成功。我可以创建一个循环来获得所需的输出,我想知道是否有矢量化的方法来实现这一点。

fillInTheBlanks <- function(v, n=3) {
  result <- v
  counter0 <- 1
  for(i in 2:length(v)) {
    value <- v[i]
    if (is.na(value)) {
      if (counter0 > n) {
        result[i] <- v[i]
      } else {  
        result[i] <- result[i-1]
        counter0 <- counter0 + 1
      } }   
    else {
      result[i] <- v[i] 
      counter0 <- 1
    }
  }
  return(result)
}

由于

5 个答案:

答案 0 :(得分:10)

这是另一种方式:

l <- cumsum(! is.na(x))
c(NA, x[! is.na(x)])[replace(l, ave(l, l, FUN=seq_along) > 4, 0) + 1]
# [1]  1  1  1  1  5  5  5  5 NA NA 11 12 12 12 12 NA NA NA 19 20

修改:我之前的回答要求x没有重复项。目前的答案没有。

<强>基准

x <- rep(x, length.out=1e4)

plourde <- function(x) {
    l <- cumsum(! is.na(x))
    c(NA, x[! is.na(x)])[replace(l, ave(l, l, FUN=seq_along) > 4, 0) + 1]
}

agstudy <- function(x) {
    unlist(sapply(split(coredata(x),cumsum(!is.na(x))),
           function(sx){
             if(length(sx)>3) 
               sx[2:4] <- rep(sx[1],3)
             else sx <- rep(sx[1],length(sx))
             sx
           }))
}

microbenchmark(plourde(x), agstudy(x))
# Unit: milliseconds
#        expr   min     lq median     uq   max neval
#  plourde(x)  5.30  5.591  6.409  6.774 57.13   100
#  agstudy(x) 16.04 16.249 16.454 17.516 20.64   100

答案 1 :(得分:4)

另一个想法,除非我错过了什么,似乎是有效的:

na_locf_until = function(x, n = 3)
{
   wnn = which(!is.na(x))  
   inds = sort(c(wnn, (wnn + n+1)[which((wnn + n+1) < c(wnn[-1], length(x)))]))
   c(rep(NA, wnn[1] - 1), 
     as.vector(x)[rep(inds, c(diff(inds), length(x) - inds[length(inds)] + 1))])
}
na_locf_until(x)
#[1]  1  1  1  1  5  5  5  5 NA NA 11 12 12 12 12 NA NA NA 19 20

答案 2 :(得分:3)

不使用na.locf,但想法是将xts按非缺失值组拆分,然后对于每个组,用第一个值替换3个第一个值(在非误读之后)。它是一个循环,但由于它仅应用于组,因此它应该比所有值上的简单循环更快。

zz <- 
unlist(sapply(split(coredata(x),cumsum(!is.na(x))),
       function(sx){
         if(length(sx)>3) 
           sx[2:4] <- rep(sx[1],3)
         else sx <- rep(sx[1],length(sx))
         sx
       }))
## create the zoo object since , the latter algorithm is applied only to the values 
zoo(zz,index(x))

2014-09-20 2014-09-21 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-26 2014-09-27 2014-09-28 2014-09-29 2014-09-30 2014-10-01 2014-10-02 
         1          1          1          1          5          5          5          5         NA         NA         11         12         12 
2014-10-03 2014-10-04 2014-10-05 2014-10-06 2014-10-07 2014-10-08 2014-10-09 
        12         12         NA         NA         NA         19         20 

答案 3 :(得分:2)

SELECT count(student_id) as i from students INNER JOIN patient ON students.sid=patient.student_id INNER JOIN ailment ON atient.ailment_id=ailment.ailment_id WHERE patient.date_created BETWEEN @d1 AND @d2" 中实现此目的的最干净的方法可能是使用连接语法:

data.table

*此解决方案取决于日期列,并且该列是连续的

答案 4 :(得分:1)

这个在data.table中玩耍的方法很黑:

np1 <- 3 + 1
dt[, 
   x_filled := x[c(rep(1, min(np1, .N)), rep(NA, max(0, .N - np1)))],
   by = cumsum(!is.na(x))]
# Or slightly simplified:
dt[, 
   x_filled := ifelse(rowid(x) < 4, x[1], x[NA]),
   by = cumsum(!is.na(x))]

> dt
          date  x x_filled
 1: 2019-02-14  1        1
 2: 2019-02-15 NA        1
 3: 2019-02-16 NA        1
 4: 2019-02-17 NA        1
 5: 2019-02-18  5        5
 6: 2019-02-19 NA        5
 7: 2019-02-20 NA        5
 8: 2019-02-21 NA        5
 9: 2019-02-22 NA       NA
10: 2019-02-23 NA       NA
11: 2019-02-24 11       11
12: 2019-02-25 12       12
13: 2019-02-26 NA       12
14: 2019-02-27 NA       12
15: 2019-02-28 NA       12
16: 2019-03-01 NA       NA
17: 2019-03-02 NA       NA
18: 2019-03-03 NA       NA
19: 2019-03-04 19       19
20: 2019-03-05 20       20

我们基于这样的事实:用NA设置子向量会返回NA

数据/包装

library(zoo)
library(data.table)
x <- zoo(1:20, Sys.Date() + 1:20)
x[c(2:4, 6:10, 13:18)] <- NA
dt <- data.table(date = index(x), x = as.integer(x))