繁体   English   中英

将矩阵转换回xts

[英]Convert a matrix back to xts

我有一个矩阵,表示随时间变化的一堆相关矩阵的特征值。

我的矩阵中有一列引用了时间的列,但据我所知,它不是时间序列或xts对象。

最终,我希望将此矩阵转换为数据帧或xts对象的格式,这使我可以绘制N个最大的特征值随时间的变化。

我如何将这个矩阵转换成这样的格式,我猜XTS是更可取的,因为它是时间序列表示形式?

我尝试了以下方法,但无法正常工作:

time.index <- as.POSIXct(colnames(eigen))
eigenXTS <- as.xts(eigen, order.by = time.index)

但是我回到了一个错误

Error in xts(x, order.by = order.by, frequency = frequency, ...) : 
  NROW(x) must match length(order.by)

我的数据如下所示:

> class(eigen)
[1] "matrix"

> str(eigen)
 num [1:12, 1:1334] 4.461 2.292 2.216 1.425 0.839 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:1334] "2017-01-20 18:45:00" "2017-01-20 19:00:00" "2017-01-20 19:15:00" "2017-01-20 19:30:00" ...

> dim(eigen)
[1]   12 1334

> eigen[1:4,1:4]
     2017-01-20 18:45:00 2017-01-20 19:00:00 2017-01-20 19:15:00 2017-01-20 19:30:00
[1,]            4.461059            4.774866            4.658013            4.841987
[2,]            2.291520            2.330239            2.101630            2.145122
[3,]            2.215749            2.183941            1.935904            1.861954
[4,]            1.424662            1.277794            1.750168            1.762004

谁能指出我如何最好地解决这个问题的方向?

我认为您需要先转置矩阵并将其转换为data.frame然后再将其转换为xts以便可以将行作为记录(观察),将列作为变量。

> dput(eigen)
structure(list(`2017-01-20 18:45:00` = c("4.461059", "2.291520", 
"2.215749", "1.424662"), `2017-01-20 19:00:00` = c("4.774866", 
"2.330239", "2.183941", "1.277794"), `2017-01-20 19:15:00` = c("4.658013", 
"2.101630", "1.935904", "1.750168"), `2017-01-20 19:30:00` = c("4.841987", 
"2.145122", "1.861954", "1.762004")), .Names = c("2017-01-20 18:45:00", 
"2017-01-20 19:00:00", "2017-01-20 19:15:00", "2017-01-20 19:30:00"
), row.names = c(NA, 4L), class = "data.frame")

> eigen <- as.data.frame(t(eigen))
                          V1       V2       V3       V4
2017-01-20 18:45:00 4.461059 2.291520 2.215749 1.424662
2017-01-20 19:00:00 4.774866 2.330239 2.183941 1.277794
2017-01-20 19:15:00 4.658013 2.101630 1.935904 1.750168
2017-01-20 19:30:00 4.841987 2.145122 1.861954 1.762004

> xts_eigen <- xts::xts(eigen,order.by = as.POSIXct(rownames(eigen)))
                          V1       V2       V3       V4
2017-01-20 18:45:00 4.461059 2.291520 2.215749 1.424662
2017-01-20 19:00:00 4.774866 2.330239 2.183941 1.277794
2017-01-20 19:15:00 4.658013 2.101630 1.935904 1.750168
2017-01-20 19:30:00 4.841987 2.145122 1.861954 1.762004

> class(xts_eigen)
[1] "xts" "zoo"

as.xts预计rownames矩阵的是时间戳。 在你的情况下, colnameseigen包含时间戳。 因此,您需要在调用as.xts之前转置eigen

xeigen <- as.xts(t(eigen))
xeigen
#                         [,1]     [,2]     [,3]     [,4]
# 2017-01-20 18:45:00 4.461059 2.291520 2.215749 1.424662
# 2017-01-20 19:00:00 4.774866 2.330239 2.183941 1.277794
# 2017-01-20 19:15:00 4.658013 2.101630 1.935904 1.750168
# 2017-01-20 19:30:00 4.841987 2.145122 1.861954 1.762004

由于xts对象只是具有时间索引的矩阵,因此无需将矩阵强制为data.frame。 这样做将意味着as.xts必须将其强制返回矩​​阵。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM