I have a dataframe containing the start and end timestamps of events. I would like to create a sequence of timestamps to fill the dates between these events. Here's the structure of my data:
dat <- structure(list(event_id = 1:2,
start_time = structure(c(1617346800,1617348000),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),
end_time = structure(c(1617347400, 1617348300),
class = c("POSIXct", "POSIXt"), tzone = "UTC")),
class = "data.frame", row.names = c(NA, -2L))
What I'm hoping to do is lengthen the data frame so there are as many rows as there are minutes between the two events. So you'd end up with something like this:
final <- structure(list(event_id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 2L, 2L, 2L, 2L,
2L), start_time = structure(c(1617346800, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1617348000,
NA, NA, NA, NA, NA), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
end_time = structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 1617347400, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 1617348300), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
record_time = c("2/4/2021 7:00", "2/4/2021 7:01", "2/4/2021 7:02",
"2/4/2021 7:03", "2/4/2021 7:04", "2/4/2021 7:05", "2/4/2021 7:06",
"2/4/2021 7:07", "2/4/2021 7:08", "2/4/2021 7:09", "2/4/2021 7:10",
"2/4/2021 7:11", "2/4/2021 7:12", "2/4/2021 7:13", "2/4/2021 7:14",
"2/4/2021 7:15", "2/4/2021 7:16", "2/4/2021 7:17", "2/4/2021 7:18",
"2/4/2021 7:19", "2/4/2021 7:20", "2/4/2021 7:21", "2/4/2021 7:22",
"2/4/2021 7:23", "2/4/2021 7:24", "2/4/2021 7:25")), class = "data.frame", row.names = c(NA,
-26L))
So far what I've come up with is creating a dataframe of the full sequence:
timeline <- as.POSIXct(
seq.POSIXt(from = min(dat$start_time),
to = max(dat$end_time), by = "min"))
From here, I'm stuck as to how to join the two. Using a for
loop I can make a list of sequences, though at that point, I'm still not sure how to get both these things together (also, I'm sure it could be done better with purrr
but I'm not good with purrr
yet).
event_timelines <- list()
for (row in 1:nrow(dat)) {
event_timelines[[row]] <- seq.POSIXt(from = dat[row,]$start_time,
to = dat[row,]$end_time,
by = "min")
}
Thanks!