首页 > 解决方案 > 从 xts 对象查询 index() 时如何检索时间?

问题描述

我有:

> dput(head(data))
structure(list(Gmt.time = c("01.06.2015 00:00", "01.06.2015 00:01", 
"01.06.2015 00:02", "01.06.2015 00:03", "01.06.2015 00:04", "01.06.2015 00:05"
), Open = c(0.88312, 0.88337, 0.88377, 0.88412, 0.88393, 0.8838
), High = c(0.88337, 0.88378, 0.88418, 0.88418, 0.88393, 0.88393
), Low = c(0.883, 0.88337, 0.88374, 0.88394, 0.88368, 0.88362
), Close = c(0.88337, 0.88375, 0.88412, 0.88394, 0.8838, 0.88393
), Volume = c(83.27, 100.14, 117.18, 52.53, 77.69, 91.63)), .Names = c("Gmt.time", 
"Open", "High", "Low", "Close", "Volume"), row.names = c(NA, 
6L), class = "data.frame")

我转换成xts

data_xts <- xts(head(data[,2:6]), as.POSIXct(head(data[,1]), format='%d.%m.%Y %H:%M'))

> str(data_xts)
An ‘xts’ object on 2015-06-01/2015-06-01 00:05:00 containing:
  Data: num [1:6, 1:5] 0.883 0.883 0.884 0.884 0.884 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:5] "Open" "High" "Low" "Close" ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
 NULL
> 

我尝试检索完整的索引名称和时间:

index(data_xts[1,])

返回:

> index(data_xts[1,])
[1] "2015-06-01 BST"

我的 dput 中的第一个元素有“01.06.2015 00:00”

我需要我的输出包括“00:00”

提前致谢

标签: rdatetime-seriesxtsposixct

解决方案


时间部分00:00用于第一个索引。如果需要,我们可以format将其作为字符串获取

format(index(data_xts[1,]), "%Y-%m-%d %H:%M")
#[1] "2015-06-01 00:00"

推荐阅读