R 对时间进行舍入

发布于 2024-12-21 15:29:10 字数 238 浏览 2 评论 0原文

我有一个包含以下格式的一系列时间的数据框:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯然 2024-12-28 15:29:10

假设我们有这样的输入数据:

DF <- data.frame(times = c("08:09:23.079", "08:30:13.062"), values = 1:2)

为了简单起见,我们假设每分钟最多有一个时间点(我们展示了一种在没有此限制的情况下稍长的替代方案):

library(zoo)
library(chron)

# this assumes we want to store times to the second
tt <- times(as.character(DF$times))
z <- zoo(DF$values, tt)

plot(z, xaxt = "n")

# custom axis - assumes sufficiently many points to get reasonable graph
# round tick mark locations to the minute and remove the seconds from label
axt <- trunc(times(axTicks(1)), "min")
axis(1, at = axt, lab = sub(":..$", "", axt))

上述创建方法z 也可以替换为这个。无论每分钟是否有多个点,它都会起作用,因为它将它们聚合到分钟:

# with this z we will be store times to the minute
z <- read.zoo(DF, FUN = function(x) trunc(times(as.character(x)), "min"), 
      aggregate = mean)

编辑:绘图和截断。

Suppose we have this input data:

DF <- data.frame(times = c("08:09:23.079", "08:30:13.062"), values = 1:2)

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):

library(zoo)
library(chron)

# this assumes we want to store times to the second
tt <- times(as.character(DF$times))
z <- zoo(DF$values, tt)

plot(z, xaxt = "n")

# custom axis - assumes sufficiently many points to get reasonable graph
# round tick mark locations to the minute and remove the seconds from label
axt <- trunc(times(axTicks(1)), "min")
axis(1, at = axt, lab = sub(":..$", "", axt))

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:

# with this z we will be store times to the minute
z <- read.zoo(DF, FUN = function(x) trunc(times(as.character(x)), "min"), 
      aggregate = mean)

EDIT: plotting and truncation.

蒗幽 2024-12-28 15:29:10

冒着被称为死灵法师的风险,我会回答这个问题,因为我认为这种情况经常出现。

如果您将时间序列数据转换为 xts 格式,请按以下步骤操作。这里要用到的函数是align.time

> head(GBPJPY)
                    GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close
2009-05-01 00:14:59     146.387     146.882    146.321      146.620
2009-05-01 00:29:54     146.623     146.641    146.434      146.579
2009-05-01 00:44:59     146.579     146.908    146.570      146.810
2009-05-01 00:59:59     146.810     146.842    146.030      146.130
2009-05-01 01:14:59     146.130     146.330    146.100      146.315
2009-05-01 01:29:57     146.315     146.382    146.159      146.201
> head(align.time(GBPJPY, 15*60))
                    GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close
2009-05-01 00:15:00     146.387     146.882    146.321      146.620
2009-05-01 00:30:00     146.623     146.641    146.434      146.579
2009-05-01 00:45:00     146.579     146.908    146.570      146.810
2009-05-01 01:00:00     146.810     146.842    146.030      146.130
2009-05-01 01:15:00     146.130     146.330    146.100      146.315
2009-05-01 01:30:00     146.315     146.382    146.159      146.201

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 is align.time

> head(GBPJPY)
                    GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close
2009-05-01 00:14:59     146.387     146.882    146.321      146.620
2009-05-01 00:29:54     146.623     146.641    146.434      146.579
2009-05-01 00:44:59     146.579     146.908    146.570      146.810
2009-05-01 00:59:59     146.810     146.842    146.030      146.130
2009-05-01 01:14:59     146.130     146.330    146.100      146.315
2009-05-01 01:29:57     146.315     146.382    146.159      146.201
> head(align.time(GBPJPY, 15*60))
                    GBPJPY.Open GBPJPY.High GBPJPY.Low GBPJPY.Close
2009-05-01 00:15:00     146.387     146.882    146.321      146.620
2009-05-01 00:30:00     146.623     146.641    146.434      146.579
2009-05-01 00:45:00     146.579     146.908    146.570      146.810
2009-05-01 01:00:00     146.810     146.842    146.030      146.130
2009-05-01 01:15:00     146.130     146.330    146.100      146.315
2009-05-01 01:30:00     146.315     146.382    146.159      146.201
把梦留给海 2024-12-28 15:29:10
as.zoo(sapply(timer3,substring,1,5))
or as.xts?

也许查看更大的数据样本会有所帮助。

as.zoo(sapply(timer3,substring,1,5))
or as.xts?

Maybe looking at a bigger sample of your data would help.

对你而言 2024-12-28 15:29:10

两个步骤:1)因子转字符:as.character() 2) 字符转POSIXct:strptime()

Two steps: 1) Factor to character: as.character() 2) character to POSIXct: strptime()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文