在 R 中创建一个时间间隔为 5 分钟的 24 小时向量 [英] Create a 24 hour vector with 5 minutes time interval in R

查看:30
本文介绍了在 R 中创建一个时间间隔为 5 分钟的 24 小时向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 R 中创建一个 24 小时的向量,时间间隔为 5 分钟,或者像这样的整数格式:

Is there any way to create in R a 24 hour vector with 5 minutes time interval from scratch or from an integer format like this:

2355 对应于 23:55

2355 which would correspond to 23:55

155 对应于 1:55

155 which would correspond to 1:55

等等.

基本上我想要的是一个从 00:00 到 23:55 的向量,这样我就可以绘制一个图形,其中包含与每个时间间隔对应的数据.

Basically what I want is a vector with from 00:00 to 23:55 so I can plot a graphic with data corresponding to each time interval.

推荐答案

有一个 seq.POSIXt 函数,它具有很好的属性,by 参数将被解析为数值区间"的意思.如果您使用 format(z, "%H%M", tz="GMT") 打印结果,它将按需要显示:

There is a seq.POSIXt function which has the nice property that the by argument will get parsed for "numeric interval" meaning. If you print the results with format(z, "%H%M", tz="GMT") it will appear as desired:

format( seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min"),
          "%H%M", tz="GMT")
  [1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035" "0040" "0045" "0050"
 [12] "0055" "0100" "0105" "0110" "0115" "0120" "0125" "0130" "0135" "0140" "0145"
 [23] "0150" "0155" "0200" "0205" "0210" "0215" "0220" "0225" "0230" "0235" "0240"
 [34] "0245" "0250" "0255" "0300" "0305" "0310" "0315" "0320" "0325" "0330" "0335"
 [45] "0340" "0345"  snipped the rest.

除非您在格林威治(或者是巴黎)的 360/48 度范围内,否则您需要输入 tz="GMT" 以便您的时区的偏移量不会出现.没有它,这对我来说产生了一个从1700"开始的序列.如果您需要保持名称,您可以将内部结果分配给一个名称,但它不是一个字符值,而是一个 POSIXct 对象:

Unless you are within 360/48 degrees of Greenwich (or is it Paris) you need to put in the tz="GMT" so that the offset for your timezone does not appear. Without that this produced a sequence starting at "1700" for me. You could assign the inner result to a name if you needed to keep it arount but it would not be a character value but rahter a POSIXct object:

z <- seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min")
> z[1]
[1] "2014-09-09 17:00:00 PDT"

这篇关于在 R 中创建一个时间间隔为 5 分钟的 24 小时向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆