Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp R Sequence Milliseconds

Tags:

r

I asked a question previously (see: Sequence of Timestamps with Milliseconds) however for some reason when my time starts as 00:00:00 my code does not work.

I would like to get a sequence of 10hz times from one time to another. However this code is giving me:

1   2018-06-01 00:00:00.000
2   2018-06-01 00:00:00.101
3   2018-06-01 00:00:00.202
4   2018-06-01 00:00:00.303
5   2018-06-01 00:00:00.404

When I need:

1   2018-06-01 00:00:00.000
2   2018-06-01 00:00:00.100
3   2018-06-01 00:00:00.200
4   2018-06-01 00:00:00.300
5   2018-06-01 00:00:00.400

Code:

options(digits.secs=3)
Time1 ="2018-06-01 00:00:00"
Time2 ="2018-06-01 00:00:10"
Time1 =as.POSIXct(Time1, format="%Y-%m-%d %H:%M:%OS", tz='UTC')
Time2 =as.POSIXct(Time2, format="%Y-%m-%d %H:%M:%OS", tz='UTC')
library(stringr)
dif_T2_T1 <- difftime(Time1, Time2, units = 'secs')
pattern <- '(\\d)+'
n <- as.numeric(str_extract(dif_T2_T1, pattern = pattern)) * 10
df_blank  <- data.frame(Timestamp = as.character(seq.POSIXt(Time1, Time2, units = 'seconds', length.out = n)))
like image 563
megmac Avatar asked Jun 13 '20 22:06

megmac


1 Answers

Due to rounding down issues with milliseconds in R (see this post), you need to add a tiny fractional amount to the vector. And the length.out should be n+1, not n.

df_blank  <- data.frame(Timestamp = seq.POSIXt(Time1, Time2, length.out=n+1) + 0.0001)
head(df_blank)
#                Timestamp
#1 2018-06-01 00:00:00.000
#2 2018-06-01 00:00:00.100
#3 2018-06-01 00:00:00.200
#4 2018-06-01 00:00:00.300
#5 2018-06-01 00:00:00.400
#6 2018-06-01 00:00:00.500

Without the addition of the tiny amount, you can see the problem.

df_blank  <- data.frame(Timestamp = seq.POSIXt(Time1, Time2, length.out=n+1))

head(format(df_blank, "%Y-%m-%d %H:%M:%OS6"))
#                   Timestamp
#1 2018-06-01 00:00:00.000000
#2 2018-06-01 00:00:00.099999
#3 2018-06-01 00:00:00.200000
#4 2018-06-01 00:00:00.299999
#5 2018-06-01 00:00:00.400000
#6 2018-06-01 00:00:00.500000

And without the formatting, you see what appears to be a very strange sequence.

head(df_blank)
#              Timestamp
#1 2018-06-01 00:00:00.0
#2 2018-06-01 00:00:00.0
#3 2018-06-01 00:00:00.2
#4 2018-06-01 00:00:00.2
#5 2018-06-01 00:00:00.4
#6 2018-06-01 00:00:00.5
like image 191
Edward Avatar answered Nov 05 '22 07:11

Edward