R 对时间进行舍入
我有一个包含以下格式的一系列时间的数据框:
08:09:23.079
> class(timer3)
[1] "factor"
我想将它们舍入/转换为这种格式:
08:09
最终目标是将它们用作绘图的 x 轴值,所以我假设它们需要转到某种类型的时间格式(zoo、as.Date 等)。
有什么建议吗?
I have a data frame with a series of times in the following format:
08:09:23.079
> class(timer3)
[1] "factor"
I would like to round/convert them to this format:
08:09
The end goal is to use them as values for the x-axis of a plot so I assume they would need to go to some type of time format (zoo, as.Date, etc.).
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设我们有这样的输入数据:
为了简单起见,我们假设每分钟最多有一个时间点(我们展示了一种在没有此限制的情况下稍长的替代方案):
上述创建方法z 也可以替换为这个。无论每分钟是否有多个点,它都会起作用,因为它将它们聚合到分钟:
编辑:绘图和截断。
Suppose we have this input data:
To keep it simple lets assume that there is at most one time point per minute (we show an alternative that is slightly longer afterwards without this restriction):
The above method of creating z could alternately be replaced with this. It works whether or not there is more than one point per minute as it aggregates them to the minute:
EDIT: plotting and truncation.
冒着被称为死灵法师的风险,我会回答这个问题,因为我认为这种情况经常出现。
如果您将时间序列数据转换为
xts
格式,请按以下步骤操作。这里要用到的函数是align.time
At risk of being called necromancer, I will answer this question as I think this situation arises quite often.
Here is how to do it if you convert your timeseries data in
xts
format. The function to be used here isalign.time
也许查看更大的数据样本会有所帮助。
Maybe looking at a bigger sample of your data would help.
两个步骤:1)因子转字符:as.character() 2) 字符转POSIXct:strptime()
Two steps: 1) Factor to character: as.character() 2) character to POSIXct: strptime()