如何在R中从xts更改为矩阵时保留索引/日期

时间:2017-02-14 05:05:16

标签: r xts

所以我有一个看起来像这样的xts:

            EUR.CLOSE EUR.CLOSE.1      EMA RUB.CLOSE RUB.CLOSE.1    EMA.1 COP.CLOSE COP.CLOSE.1   EMA.2 MXN.CLOSE
2016-08-30 -0.7176776   0.1507033 42.50321 0.6565255   0.1969145 52.73467 0.4499486   -1.517971 47.0703  1.618209
           MXN.CLOSE.1    EMA.3  INR.CLOSE INR.CLOSE.1    EMA.4 KRW.CLOSE KRW.CLOSE.1    EMA.5   PHP.CLOSE PHP.CLOSE.1
2016-08-30   0.6377586 59.46614 0.06742503  0.04767418 51.04673 0.7059685 -0.02246669 52.88059 -0.06188119  -0.7181662
              EMA.6
2016-08-30 45.22928
> nt[,1]

在这里输入:

structure(c(-0.717677627452784, 0.150703341994461, 42.5032053202923, 
0.656525491646624, 0.196914493557725, 52.7346708652452, 0.44994857730542, 
-1.51797051765522, 47.0703023915683, 1.61820941470502, 0.637758577228606, 
59.4661365407602, 0.0674250335669636, 0.0476741757424079, 51.0467264878632, 
0.705968496624654, -0.0224666928598931, 52.8805945280585, -0.0618811881188627, 
-0.718166164442612, 45.2292753580903), .Dim = c(1L, 21L), .Dimnames = list(
    NULL, c("EUR.CLOSE", "EUR.CLOSE.1", "EMA", "RUB.CLOSE", "RUB.CLOSE.1", 
    "EMA.1", "COP.CLOSE", "COP.CLOSE.1", "EMA.2", "MXN.CLOSE", 
    "MXN.CLOSE.1", "EMA.3", "INR.CLOSE", "INR.CLOSE.1", "EMA.4", 
    "KRW.CLOSE", "KRW.CLOSE.1", "EMA.5", "PHP.CLOSE", "PHP.CLOSE.1", 
    "EMA.6")), index = structure(1472533200, tzone = "", tclass = c("POSIXct", 
"POSIXt")), .indexTZ = "", class = c("xts", "zoo"), .indexCLASS = c("POSIXct", 
"POSIXt"), tclass = c("POSIXct", "POSIXt"), tzone = "")

我想将其转换为:

   Dates             % dev from 20MA % dev from 20MA      RSI
   2016-08-30  EUR    -0.71767763      0.15070334 42.50321
   2016-08-30  RUB      0.65652549      0.19691449 52.73467
   2016-08-30  COP      0.44994858     -1.51797052 47.07030
   2016-08-30  MXN      1.61820941      0.63775858 59.46614
   2016-08-30  INR      0.06742503      0.04767418 51.04673
   2016-08-30  KRW      0.70596850     -0.02246669 52.88059
   2016-08-30  PHP     -0.06188119     -0.71816616 45.22928

我尝试过以下代码:

out.trend <- matrix(new.trend(MergedData), ncol = 3, byrow = TRUE, 
                    dimnames = list(paste0(cursig), c("% dev from 20MA", "% dev from 20MA", "RSI")))

但是这导致上面的矩阵没有日期。无论如何我可以从我的xts向量中追加日期吗?

1 个答案:

答案 0 :(得分:2)

如果要保留日期和字符变量,则需要data.frame,而不是矩阵,它只能包含一种类型。转换后需要进行大量的清洁工作,但没有什么不寻常的。这里使用tidyverse,但如果你愿意,可以转换成另一种语法。 x是上面的xts对象。

library(tidyverse)

x %>% as.data.frame() %>% 
    rownames_to_column('date') %>% 
    gather(var, val, -date) %>% 
    separate(var, c('currency', 'var'), extra = 'merge') %>% 
    mutate(date = as.Date(date), 
           var = sub('^\\d$', 'RSI', var), 
           var = coalesce(var, 'RSI'), 
           currency = na_if(currency, 'EMA')) %>% 
    fill(currency) %>% 
    spread(var, val)

#>         date currency       CLOSE     CLOSE.1      RSI
#> 1 2016-08-30      COP  0.44994858 -1.51797052 47.07030
#> 2 2016-08-30      EUR -0.71767763  0.15070334 42.50321
#> 3 2016-08-30      INR  0.06742503  0.04767418 51.04673
#> 4 2016-08-30      KRW  0.70596850 -0.02246669 52.88059
#> 5 2016-08-30      MXN  1.61820941  0.63775858 59.46614
#> 6 2016-08-30      PHP -0.06188119 -0.71816616 45.22928
#> 7 2016-08-30      RUB  0.65652549  0.19691449 52.73467