我试图弄清楚如何使用小于1秒的单位将时间间隔添加到R中的日期向量。如您所见,填充一秒钟的间隔是可行的,但是小数点会失败。我环顾四周,似乎R中没有“毫秒”单位。是否可以通过某种方式填充间隔小于1秒的时间向量?
> require(padr)
> require(dplyr)
> options(digits.secs=3)
> start = as.POSIXct("2020-01-28 03:31:22.209 EST", format = "%Y-%m-%d %H:%M:%OS")
> end = as.POSIXct("2020-01-28 05:31:22.209 EST", format = "%Y-%m-%d %H:%M:%OS")
>
> minz <- seq(start, end, units = "minutes", by = "1 min")
> head(minz)
[1] "2020-01-28 03:31:22.209 EST" "2020-01-28 03:32:22.209 EST" "2020-01-28 03:33:22.209 EST" "2020-01-28 03:34:22.209 EST"
[5] "2020-01-28 03:35:22.209 EST" "2020-01-28 03:36:22.209 EST"
> secz <- as.data.frame(minz) %>% pad('1 sec')
> head(secz)
minz
1 2020-01-28 03:31:22.209
2 2020-01-28 03:31:23.209
3 2020-01-28 03:31:24.209
4 2020-01-28 03:31:25.209
5 2020-01-28 03:31:26.209
6 2020-01-28 03:31:27.209
> half_secz <- as.data.frame(minz) %>% pad('0.5 sec')
Error in seq.int(0, to0 - from, by) : invalid '(to - from)/by'
> head(secz)
minz
1 2020-01-28 03:31:22.209
2 2020-01-28 03:31:23.209
3 2020-01-28 03:31:24.209
4 2020-01-28 03:31:25.209
5 2020-01-28 03:31:26.209
6 2020-01-28 03:31:27.209
我们可以使用seq
,因为它会将by
参数指定为0.5。
start = as.POSIXct("2020-01-28 03:31:22.209", format = "%Y-%m-%d %H:%M:%OS",tz = "UTC")
end = as.POSIXct("2020-01-28 05:31:22.209", format = "%Y-%m-%d %H:%M:%OS",tz = "UTC")
seq(start, end, by = 0.5) %>% head
#[1] "2020-01-28 03:31:22.209 UTC" "2020-01-28 03:31:22.709 UTC"
#[3] "2020-01-28 03:31:23.209 UTC" "2020-01-28 03:31:23.709 UTC"
#[5] "2020-01-28 03:31:24.209 UTC" "2020-01-28 03:31:24.709 UTC"
或者如果您想在secz
数据帧中使用它,则>
tidyr::complete(secz, minz = seq(min(minz), max(minz), by = 0.5))
# minz
# <dttm>
# 1 2020-01-28 03:31:22.209
# 2 2020-01-28 03:31:22.709
# 3 2020-01-28 03:31:23.209
# 4 2020-01-28 03:31:23.709
# 5 2020-01-28 03:31:24.209
# 6 2020-01-28 03:31:24.709
# 7 2020-01-28 03:31:25.209
# 8 2020-01-28 03:31:25.709
# 9 2020-01-28 03:31:26.209
#10 2020-01-28 03:31:26.709
# … with 14,391 more rows