0

I am trying to use to.period to convert milliseconds data into regular seconds level data - however the final output has trailing milliseconds

dput(head(x$Mid,20))
structure(c(3228.5, 3228.5, 3228.5, 3229, 3229, 3229, 3229.5, 
3229.5, 3229.5, 3229, 3229.5, 3229.5, 3229.5, 3230, 3228.5, 3231.5, 
3231, 3231, 3230, 3231), class = c("xts", "zoo"), index = structure(c(1543804206.41, 
1543804206.55, 1543804206.63, 1543804206.68, 1543804206.75, 1543804206.8, 
1543804206.85, 1543804206.93, 1543804206.97, 1543804207.03, 1543804207.07, 
1543804207.1, 1543804207.14, 1543804207.22, 1543804207.29, 1543804207.44, 
1543804207.7, 1543804207.75, 1543804207.88, 1543804207.93), tzone = "", tclass = c("POSIXct", 
"POSIXt")), .Dim = c(20L, 1L), .Dimnames = list(NULL, "Mid"))


dput(to.period(head(x$Mid,20),"seconds"))
structure(c(3228.5, 3229, 3229.5, 3231.5, 3228.5, 3228.5, 3229.5, 
3231), .Dim = c(2L, 4L), .Dimnames = list(NULL, c("head(x$Mid, 20).Open", 
"head(x$Mid, 20).High", "head(x$Mid, 20).Low", "head(x$Mid, 20).Close"
)), index = structure(c(1543804206.97, 1543804207.93), tzone = "", tclass = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"))

How to make the to.period output on timestamps round to the seconds.

shoonya
  • 292
  • 1
  • 10

1 Answers1

1

to.period works with endpoints and using to.period in the way you are doing will take the last value of the second. to.period does not round the values. If you want that you could use the round_date or floor_date function from lubridate to get what you want.

A small example based on your x$MID:

t <- to.period(x, "seconds", OHLC = FALSE)
t
                          Mid
2018-12-03 03:30:06.97 3229.5
2018-12-03 03:30:07.93 3231.0

library(lubridate)
index(t) <- floor_date(index(t), "1s") # replace index with floor value

t
                       Mid
2018-12-03 03:30:06 3229.5
2018-12-03 03:30:07 3231.0
phiver
  • 23,048
  • 14
  • 44
  • 56