【问题标题】:Perform Operations on the same day using XTS in R在 R 中使用 XTS 在同一天执行操作
【发布时间】:2017-10-17 09:37:31
【问题描述】:

我有一个希望是直截了当的问题。我有一个类似于以下内容的xts 对象:

                        | MarketPrice  |
----------------------------------------
2007-05-04 10:15:33.546 |   5.32       |
----------------------------------------
2007-05-04 10:16:42.100 |   5.31       |
----------------------------------------
2007-05-04 10:17:27.546 |   NA         |
----------------------------------------
2007-05-04 10:20:50.871 |   5.35       |
----------------------------------------
2007-05-04 10:21:38.652 |   5.37       |

基本上,我想在某个时间之前立即找到MarketPrice 索引,同时也省略NA 值。例如,假设我们从时间2007-05-04 10:20:50.871 开始,该时间在对象中的索引为 4。所以这意味着在此时间之前的市场价格是5.31,它在对象中的索引为 2。为了执行这个任务,我编写了一个类似于以下的函数:

 MPFunction <- function(t,df){

 ind <- t
 while(t>1){
     t=t-1
     if ( (index(df[t]) != index(df[ind])) && !(is.na(df[t,"MarketPrice"])))  {

    return(t)
   }
}
}

这将执行任务,因为 IF 语句中的第一个条件检查以确保 xts 对象的索引中的时间不同,而第二个条件检查以确保没有 NA 值在MarketPrice 专栏。

但是,当我查看几天时,我现在遇到了一个问题。假设我现在有一个 xts 对象,如下所示:

                          | MarketPrice  |
  ----------------------------------------
  2007-05-03 16:59:58.921 |   5.32       |
  ----------------------------------------
  2007-05-04 10:12:27.546 |   NA         |
  ----------------------------------------
  2007-05-04 10:20:50.871 |   5.35       |
  ----------------------------------------

如果我从索引 3 开始(即当时 2007-05-04 10:20:50.871),那么如果我希望在此时间之前找到第一个在 MarketPrice 列中没有 NA 值的索引,它将去索引 1,即2007-05-03 16:59:58.921。然而问题是这是在不同的日子,我想确保我只在同一天提取MarketPrice 值的索引。

基本上,我想知道是否可以在 IF 语句中对上面的MPFunction 进行快速修改,这样我就可以避免找到前一天的 MarketPrice 指数。此外,我不希望每天拆分 xts 对象,因为如果我这样做会使事情变得相当复杂。

现在,我已经有了一些关于如何解决这个问题的想法(例如使用strptime 函数来检查日期等),但是这些都是耗时的方法,所以我希望找到一种方法。快得多,所以如果有人有任何想法,我将不胜感激。提前致谢。

【问题讨论】:

    标签: r xts


    【解决方案1】:

    听起来您实际上想使用split.xts(为什么要使用拆分复杂功能?不应该,即使每天都有大量的刻度数据),然后重新组合结果:

    zz=xts(order.by = as.POSIXct(c("2007-05-03 09:59:58.921", 
                                   "2007-05-03 10:03:58.921",
                                   "2007-05-03 12:03:58.921"
                      "2007-05-04 10:15:33.546",
                     "2007-05-04 10:16:42.100",
                     "2007-05-04 10:17:27.546",
                     "2007-05-04 10:20:50.871",
                     "2007-05-04 10:21:38.652")),
      x = c(3, 4, 9,  5.32, 5.31, NA, 5.35, 5.37), dimnames = list(NULL, "MarketPrice"))
    
    > zz
    #                      MarketPrice
    # 2007-05-03 09:59:58        3.00
    # 2007-05-03 10:03:58        4.00
    # 2007-05-04 10:15:33        5.32
    # 2007-05-04 10:16:42        5.31
    # 2007-05-04 10:17:27          NA
    # 2007-05-04 10:20:50        5.35
    # 2007-05-04 10:21:38        5.37
    
    
    MPFunction <- function(x, time_window = "T10/T10:16:40") {
      #last(x[time_window, which.i= TRUE])   # get the index?
      # last returns the last row in the group selected:
      #last(x[time_window,])
      u <- x[time_window, which.i = TRUE]
    
      if (length(u) > 0) {
        # Get index which is not an NA value:
        u.na <- which(is.na(x[time_window, "MarketPrice"]))
        u2 <- u[!u %in% u.na]
        if (length(u2) > 0) {
          v <- xts(order.by = end(x[last(u2)]), x = last(u2), dimnames = list(NULL, "index.i"))        
        } else {
          v <- NULL      
        }
      } else {
        v <- NULL
      }
      v
    }
    
    # use T0/ as the start of the time window in each day for getting the index value by default. You can change this though.
    chosen_window = "T0/T10:17:29"
    
    by_day <- lapply(split(zz, f = "day"), FUN = MPFunction, time_window = chosen_window)
    
    rr <- do.call(rbind, by_day)
    
    > rr
    #                     index.i
    # 2007-05-03 10:03:58       2
    # 2007-05-04 10:16:42       2
    

    如果在感兴趣的time_window 中某一天没有值,您将获得当天的NULL,并且当天的输出 (rr) 中没有返回任何值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2016-01-26
      • 2013-04-20
      相关资源
      最近更新 更多