r - to.period() 无法正常工作 - 尾随毫秒
问题描述
我正在尝试使用 to.period 将毫秒数据转换为常规秒级数据-但是最终输出具有尾随毫秒
dput(head(x$Mid,20))
structure(c(3228.5, 3228.5, 3228.5, 3229, 3229, 3229, 3229.5,
3229.5, 3229.5, 3229, 3229.5, 3229.5, 3229.5, 3230, 3228.5, 3231.5,
3231, 3231, 3230, 3231), class = c("xts", "zoo"), index = structure(c(1543804206.41,
1543804206.55, 1543804206.63, 1543804206.68, 1543804206.75, 1543804206.8,
1543804206.85, 1543804206.93, 1543804206.97, 1543804207.03, 1543804207.07,
1543804207.1, 1543804207.14, 1543804207.22, 1543804207.29, 1543804207.44,
1543804207.7, 1543804207.75, 1543804207.88, 1543804207.93), tzone = "", tclass = c("POSIXct",
"POSIXt")), .Dim = c(20L, 1L), .Dimnames = list(NULL, "Mid"))
dput(to.period(head(x$Mid,20),"seconds"))
structure(c(3228.5, 3229, 3229.5, 3231.5, 3228.5, 3228.5, 3229.5,
3231), .Dim = c(2L, 4L), .Dimnames = list(NULL, c("head(x$Mid, 20).Open",
"head(x$Mid, 20).High", "head(x$Mid, 20).Low", "head(x$Mid, 20).Close"
)), index = structure(c(1543804206.97, 1543804207.93), tzone = "", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"))
如何使时间戳上的 to.period 输出舍入到秒。
解决方案
to.period
以您正在做的方式使用endpoints
和使用将采用第二个的最后一个值。不四舍五入值。如果你愿意,你可以使用 lubridate 的or函数来得到你想要的。to.period
to.period
round_date
floor_date
基于您的 x$MID 的一个小示例:
t <- to.period(x, "seconds", OHLC = FALSE)
t
Mid
2018-12-03 03:30:06.97 3229.5
2018-12-03 03:30:07.93 3231.0
library(lubridate)
index(t) <- floor_date(index(t), "1s") # replace index with floor value
t
Mid
2018-12-03 03:30:06 3229.5
2018-12-03 03:30:07 3231.0
推荐阅读
- android - 找不到符号 ActivityBinding
- sql - SQL查询以查找列中的值是否包含字符串中特定位置的特定子字符串
- python - 在 pandas 数据框中将每个列列表元素转换为 JSON
- javascript - Express Routes 参数来定义反应页面组件
- python - 返回 django 验证时如何格式化(简洁)日期?
- django - 如何在 Django 表单中使用时区
- nix - Nix派生中的隔离临时文件
- php - 无法在 PHP 中使用 file_get_contents() 从 URL 解析 XML
- .net-core - 模型绑定问题 - .Net Core
- r - 使用 `rmarkdown::word_document` 输出格式参数化 Rmarkdown 文档中 `reference_docx` 的路径