I have a time series data with 1 minute increments. I have written a code but with the large amount of data I have (over 1M rows), looping through each line is taking way too long. The data looks something like the below:
t0 = as.POSIXlt("2018-12-23 00:01:00")
t0 = t0+seq(60,60*10,60)
p1 = seq(5,5*10,5)
p2 = seq(7,7*10,7)
m0 = cbind(p1,p2)
rownames(m0) = as.character(t0)
Where it looks something like:
> head(m0)
p1 p2
2018-12-23 00:02:00 5 7
2018-12-23 00:03:00 10 14
2018-12-23 00:04:00 15 21
2018-12-23 00:05:00 20 28
2018-12-23 00:06:00 25 35
2018-12-23 00:07:00 30 42
I want to turn this data into 5 seconds increments by adding 11 lines (55 seconds) before each minute with the value carrying over from the latest value. So it would something like:
> new0
p1 p2
2018-12-23 00:01:05 5 7
2018-12-23 00:01:10 5 7
2018-12-23 00:01:15 5 7
2018-12-23 00:01:20 5 7
2018-12-23 00:01:25 5 7
2018-12-23 00:01:30 5 7
2018-12-23 00:01:35 5 7
2018-12-23 00:01:40 5 7
2018-12-23 00:01:45 5 7
2018-12-23 00:01:50 5 7
2018-12-23 00:01:55 5 7
2018-12-23 00:02:00 5 7
2018-12-23 00:02:05 10 14
2018-12-23 00:02:10 10 14
2018-12-23 00:02:15 10 14
2018-12-23 00:02:20 10 14
2018-12-23 00:02:25 10 14
2018-12-23 00:02:30 10 14
2018-12-23 00:02:35 10 14
2018-12-23 00:02:40 10 14
2018-12-23 00:02:45 10 14
2018-12-23 00:02:50 10 14
2018-12-23 00:02:55 10 14
2018-12-23 00:03:00 10 14
I am hoping to find some way to do it without using a loop and utilizing the efficient codes in xts and/or data.table which I am not too familiar with.
I tried using the ave
function from base R, but it is not fast enough.