Copy down last value over a daily period

后端 未结 2 1375
一个人的身影
一个人的身影 2021-01-22 05:33

I have a multi-day XTS object, and I am trying to create an indicator that once true, remains true for the rest of the day. The approach I am trying (but its not working) is com

2条回答
  •  时光说笑
    2021-01-22 06:24

    One option is

    y1 <- ave(y, as.Date(index(y)), FUN= function(x) na.locf(x, na.rm=FALSE))
    y1
    #                      [,1]
    #2010-01-05 00:00:00   NA
    #2010-01-05 00:04:00   NA
    #2010-01-05 00:08:00    1
    #2010-01-05 00:12:00    1
    #2010-01-05 00:16:00    1
    #2010-01-05 00:20:00    1
    #2010-01-06 00:00:00   NA
    #2010-01-06 00:04:00   NA
    #2010-01-06 00:08:00   NA
    
    str(y1)
    # An ‘xts’ object on 2010-01-05/2010-01-06 00:08:00 containing:
    #  Data: num [1:9, 1] NA NA 1 1 1 1 NA NA NA
    #  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
    #  Original class: 'double'  
    # xts Attributes:  
    # NULL
    
    str(y)
    #An ‘xts’ object on 2010-01-05/2010-01-06 00:08:00 containing:
    #  Data: num [1:9, 1] NA NA 1 NA NA NA NA NA NA
    #  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
    #  Original class: 'double'  
    #  xts Attributes:  
    # NULL
    

提交回复
热议问题