从xts对象查询index()时如何检索时间?

时间:2018-06-10 20:33:51

标签: r date time-series xts posixct

我有:

> 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中的第一个元素是&#34; 01.06.2015 00:00&#34;

我需要输出包含&#39; 00:00&#39;

提前致谢

1 个答案:

答案 0 :(得分:1)

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

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