r - 从 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”
提前致谢
解决方案
时间部分00:00
用于第一个索引。如果需要,我们可以format
将其作为字符串获取
format(index(data_xts[1,]), "%Y-%m-%d %H:%M")
#[1] "2015-06-01 00:00"
推荐阅读
- ansible - Ansible 成为用户不运行 /etc/profile
- r - 根据该行的另一列中的值对列中的值进行操作的更好方法?
- css - 如何在我的渐变中添加渐变不透明度?
- php - 在页面导航上清除了 Angular php-jwt 令牌
- python - Scrapy Splash formrequest.formresponse, - 停留在识别动态加载页面的表单数据
- c - 什么是 C 等价于 Matlab 的索引表达式 A(:,n)?
- angular - 日历显示“NaN”而不是“June”
- python-3.x - 将日期名称转换为数值
- java - 注释处理器和匿名类
- c++ - 如何对重叠的八叉树位置进行排序