0

The data I have deals with tests being run on electrical equipment with observations being 1 microsecond or 1/1,000,000 sec apart. The data stats when the test starts but when each observation happens. My boss wants me to add in two columns for the time data. The first time column needs to have POSIXct data that goes to the 1 millisecond or 1/1,000 sec.The second Time column will run from 0:999 to represent the microseconds. The data in the first time column needs to stay POSIXct because other programs we use read time data that is in that format. The reason I am using two time columns is because POSIXct becomes inaccurate with times smaller then microseconds.

My question is how can start at say "2002-11-12 14:10:25.120 UTC" repeat a thousand times then increase by +.001 sec? Any thoughts to do this more efficiently would also be welcome.

Chuck
  • 13
  • 1

1 Answers1

0

I think using ceiling() will be the best bet. You can take the ceiling of a progression of numbers (I used dplyr's row_number()) that you divide by some number and get increasing steps.

library(tidyverse)
df <- data.frame(date = rep(0, 100000)) %>%
    mutate(date = as.POSIXct(Sys.Date()))

df <- df %>%
    mutate(newnum = lubridate::milliseconds(ceiling(row_number() / 1000)),
           newdate = date + newnum)

To illustrate with a plot:

plot(lubridate::second(df$newdate[1:10000]))

enter image description here

Adrian Martin
  • 780
  • 7
  • 21