【问题标题】:filling empty xts object in R在R中填充空xts对象
【发布时间】:2016-05-14 08:26:13
【问题描述】:

我有一个空的 xts 对象,我想用简单的计算填充列(预定义日期 - xts 索引(日期)/ 365)。我已经能够填写第一个,问题是我有 46 列并且将来会有更多列,所以我这样做的方式不是最佳的。这是我能做的。如何填写其余 4 个(实际样本中为 46 个),而不必像本例中那样合并每一列。

创建空xts

    xts <- xts(order.by=index(xts))
    merge(xts, col1 = (dt[1] - index(xts))/365)
              col1
2010-12-31 6.512329
2011-01-03 6.504110
2011-01-04 6.501370
2011-01-05 6.498630
2011-01-06 6.495890
2011-01-07 6.493151

最终结果应该是这样的。

               col1     col2     col3     col4     col5
2010-12-31 6.512329 6.789041 7.016438 7.153425 7.287671
2011-01-03 6.504110 6.780822 7.008219 7.145205 7.279452
2011-01-04 6.501370 6.778082 7.005479 7.142466 7.276712
2011-01-05 6.498630 6.775342 7.002740 7.139726 7.273973
2011-01-06 6.495890 6.772603 7.000000 7.136986 7.271233
2011-01-07 6.493151 6.769863 6.997260 7.134247 7.268493

这里是 dt 变量为 5 个预定日期的数据。

dput(xts)
structure(numeric(0), index = structure(c(1293753600, 1294012800, 
1294099200, 1294185600, 1294272000, 1294358400), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")

dput(dt)
structure(c(17351L, 17452L, 17535L, 17585L, 17634L), class = "Date")

【问题讨论】:

    标签: r xts


    【解决方案1】:

    与其创建一堆xts对象然后通过Reduce递归合并它们,你可以直接创建一个xts对象。

    mat <- sapply(dt, function(d) (d-index(x))/365)
    res <- xts(mat, index(x))
    colnames(res) <- paste0("col", seq(ncol(res)))
    

    我个人认为这更直接。

    【讨论】:

      【解决方案2】:

      关键是使用Reduce合并大列表对象

      #Read Data
      
      #main index for first series
      mainIndex = as.Date(c("2010-12-31","2011-01-03","2011-01-04","2011-01-05","2011-01-06","2011-01-07"),format="%Y-%m-%d")
      
      referenceDates = as.Date(c("2017-07-04","2017-10-13","2018-01-04","2018-02-23","2018-04-13"),format="%Y-%m-%d")
      
      
      #Create subsequent xts objects and save as list object
      
      TS_List = lapply(1:length(referenceDates),function(x) {
      
      tsObj =xts((referenceDates[x] - mainIndex)/365,order.by=mainIndex);
      colnames(tsObj)=paste0("col",x);
      return(tsObj) 
      }) 
      
      
      #General syntax for Reduce : function(x, y) merge(x, y,by="column_column")
      #here merge uses merge.xts and common column  is index of xts objects
      
      mergeXTSfun = function(x, y) merge(x, y)
      
      merged_TS = Reduce(mergeXTSfun, TS_List )
      merged_TS
      
      #               col1     col2     col3     col4     col5
      #2010-12-31 6.512329 6.789041 7.016438 7.153425 7.287671
      #2011-01-03 6.504110 6.780822 7.008219 7.145205 7.279452
      #2011-01-04 6.501370 6.778082 7.005479 7.142466 7.276712
      #2011-01-05 6.498630 6.775342 7.002740 7.139726 7.273973
      #2011-01-06 6.495890 6.772603 7.000000 7.136986 7.271233
      #2011-01-07 6.493151 6.769863 6.997260 7.134247 7.268493
      
      
      
      DesiredOutput= read.table(text="col1     col2     col3     col4     col5
      2010-12-31 6.512329 6.789041 7.016438 7.153425 7.287671
      2011-01-03 6.504110 6.780822 7.008219 7.145205 7.279452
      2011-01-04 6.501370 6.778082 7.005479 7.142466 7.276712
      2011-01-05 6.498630 6.775342 7.002740 7.139726 7.273973
      2011-01-06 6.495890 6.772603 7.000000 7.136986 7.271233
      2011-01-07 6.493151 6.769863 6.997260 7.134247 7.268493",header=TRUE,stringsAsFactors=FALSE)
      
      
      DesiredOutput = xts(DesiredOutput,order.by=as.Date(rownames(DesiredOutput),format="%Y-%m-%d"))
      
      
      
      all.equal(merged_TS,DesiredOutput)
      #[1] "Mean relative difference: 3.67637e-08"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-25
        • 2011-08-29
        • 2012-08-05
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多