I have a large time series dataset that currently iterates through the data to change the time series data into events divided by time interval. I am looking for something more slick than iterating through, because this gets pretty slow with how large my data is. My starting dataframe looks similar to this simple one:
structure(list(Name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("a",
"b", "c"), class = "factor"), datetime = structure(c(1597203000,
1597201200, 1597199400, 1597186800, 1597185000, 1597183200, 1597197600,
1597195800, 1597194000, 1597181400, 1597179600, 1597177800, 1597192200,
1597190400, 1597188600, 1597176000, 1597174200, 1597172400), class = c("POSIXct",
"POSIXt"), tzone = ""), percent = c(0, 0, 2, 1, 0, 0, 0, 0, 3,
4, 0, 0, 0, 0, 0, 5, 0, 0)), class = "data.frame", row.names = c(NA,
-18L))
The data is half-hourly, so if a Name variable has two consecutive half hourly datetime values, I consider it to be a part of the event. I would also give some leniency, so if the data doesn't show consecutive half hourly values, but there are consecutive hour values, that would work as well. So the goal is to return a dataframe that looks like so:
structure(list(Name = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("a",
"b", "c"), class = "factor"), startdate = structure(c(1597203000,
1597197600, 1597192200, 1597186800, 1597181400, 1597176000), class = c("POSIXct",
"POSIXt"), tzone = ""), enddate = structure(c(1597199400, 1597194000,
1597188600, 1597183200, 1597177800, 1597172400), class = c("POSIXct",
"POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA,
-6L))
Thanks in advance for any snazzy solutions, I greatly appreciate it!
EDIT: The datetime values will not necessarily be in order going down the list.