在 R 中以毫秒为单位创建一系列时间戳 [英] Create a series of timestamps along milliseconds in R
问题描述
我的开始日期为2017-11-14 10:11:01 CET",结束日期为2017-11-15 01:15:59 CET".我想在这些开始和结束时间戳之间创建每个 500 毫秒的时间戳.例如.我需要一个包含以下输出的数据框
I have start date as "2017-11-14 10:11:01 CET" and end date as "2017-11-15 01:15:59 CET". I want to create timestamps of 500 milliseconds each, between these start and end timestamps. E.g. I need a dataframe containing the following output
timestamp
2017-11-14 10:11:01.000
2017-11-14 10:11:01.500
2017-11-14 10:11:02.000
2017-11-14 10:11:02.500
.
.
.
2017-11-15 01:15:59.000
不知何故,我设法使用以下代码以毫秒为单位获取开始和结束日期:
Somehow, I managed to get the start and end date in milliseconds using the following code:
start.date <- strptime("2017-11-14 10:11:01 CET", "%Y-%m-%d %H:%M:%OS")
start.date <- format(start.date, "%Y-%m-%d %H:%M:%OS3")
Output: "2017-11-14 10:11:01.000"
同样,我得到的结束日期为 2017-11-15 01:15:59.000
.我现在想使用序列函数来创建每个 500 毫秒的时间戳序列.我做了类似的事情
Similarly, I got end date as 2017-11-15 01:15:59.000
. I now want to use sequence function to create sequence of timestamps each of 500 milliseconds. I did something like
seq.POSIXt(as.POSIXct(start), as.POSIXct(end), units = "milliseconds", by = 500)
但它创建了一系列 20 秒.例如,
but it created a series of 20 seconds. e.g.,
"2017-11-14 10:11:01 CET"
"2017-11-14 10:19:21 CET"
"2017-11-14 10:27:41 CET"
.
.
.
"2017-11-15 01:11:01 CET"
有一次,我能够得到这个系列,我还想把它转换成 UNIX 时间戳.有人可以帮我解决这个问题吗?
Once, I am able to get this series, I further want to convert it into UNIX timestamps. Can someone help me to resolve this issue?
推荐答案
我不知道这是否是您要找的,但它似乎有效
I don't know if this is what you are looking for but it seems to be working
seq.POSIXt(as.POSIXct(start), as.POSIXct(end), units = "seconds", by = .5)
这篇关于在 R 中以毫秒为单位创建一系列时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!