0

So, what I am doing is creating a heatmap for x and y coordinates. But I would like to do this for every 30 minute interval. For example the first heatmap will be created using the data from "00:00:00" to "00:30:00", then the next from "00:01:00" to "00:31:00".

What I need help with is writing a for loop that can extract these rows from a larger database and then spit out the heatmap for each bracket of data. I have been told that zoo::rollapply could be useful in the process but am not sure how it works.

The database has three columns x, y, and indiv.times. x and y are the coordinate systems and indiv.times is a character variable which contains the times in the format "13:04:46" for example.

for (i in ???) {
  kde <- kde2d(x, y)
  plot_ly(z = kde$z, type = "heatmap")
}

This is the code to create the heatmap so I really just need a way to extract the 30 minute intervals.

Any help would be greatly appreciated.

Here is a sample of the database:

structure(list(x = c(224.7666, 223.3886, 131.7025, 345.333), 
    y = c(60.7657, 85.73872, 77.35342, 26.24607), indiv.times = Sys.time() +
    cumsum(60*sample(20, size = 10, replace = TRUE)), class = "data.frame", row.names = c(NA, -4L)))
Ekatef
  • 1,061
  • 1
  • 9
  • 12

1 Answers1

0

So if anyone else is interested, I created an index i that has all the times from "00:00:00" all the way to "24:00:00". Than inside the for loop you just need to extract the rows from the data frame where df[time < i + 1800 & time > i,]. Make sure your times are in the time format and not just strings. Then you can perform any adjustments in the for loop using the new extracted data frame.