Fill NA in a time series only to a limited number

前端 未结 5 1000
滥情空心
滥情空心 2020-12-01 17:08

Is there a way we can fill NAs in a zoo or xts object with limited number of NAs forward. In other words like fill

5条回答
  •  既然无缘
    2020-12-01 17:44

    Here's another way:

    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
    

    edit: my previous answer required that x have no duplicates. The current answer does not.

    benchmarks

    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
    

提交回复
热议问题