-1

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.

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.

EDIT:

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 = c("14:00:02", 
    "14:00:02", "14:00:03", "05:10:26")), class = "data.frame", row.names = c(NA, -4L))
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • This might be an interesting problem, but since we have no data to look at, it falls a little short in letting us "play". Please make this question *reproducible*. This includes sample code (including listing non-base R packages), sample data (e.g., `dput(head(x))`), and expected output. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Oct 30 '18 at 03:11
  • @r2evans just made an edit to include the first 4 rows of the database. Hope this was what you were looking for, unfortunately it seems to be terribly difficult to include tables into your questions on this site so I tried my hardest. – Seamus O'Leary Oct 30 '18 at 03:55
  • I just edited it to present a more consumable format for including data in your question. It's not the only, nor always the best, but it works. This can also be provided using programmatic building, ala `data.frame(...)`, butas always it depends on the data, your comfort, and several other factors. – r2evans Oct 30 '18 at 04:03

1 Answers1

0

One method would be to bin them by intervals using split and findInterval.

Your data are a bit short, I'll generate something to demonstrate:

set.seed(3)
df <- data.frame(
  time = Sys.time() + cumsum(60*sample(20, size=10, replace=TRUE))
)
df
#                   time
# 1  2018-10-29 21:18:10
# 2  2018-10-29 21:35:10
# 3  2018-10-29 21:43:10
# 4  2018-10-29 21:50:10
# 5  2018-10-29 22:03:10
# 6  2018-10-29 22:16:10
# 7  2018-10-29 22:19:10
# 8  2018-10-29 22:25:10
# 9  2018-10-29 22:37:10
# 10 2018-10-29 22:50:10

Let's generate some bins that will mark off every-30-minutes:

bins <- seq(
  round(min(df$time), "hour"),
  round(max(df$time) + 15*60, "hour"),
  by = "30 min"
)
bins
# [1] "2018-10-29 21:00:00 PDT" "2018-10-29 21:30:00 PDT"
# [3] "2018-10-29 22:00:00 PDT" "2018-10-29 22:30:00 PDT"
# [5] "2018-10-29 23:00:00 PDT"

And now splitting things up>

split(df, list(findInterval(df$time, bins)))
# $`1`
#                  time
# 1 2018-10-29 21:18:10
# $`2`
#                  time
# 2 2018-10-29 21:35:10
# 3 2018-10-29 21:43:10
# 4 2018-10-29 21:50:10
# $`3`
#                  time
# 5 2018-10-29 22:03:10
# 6 2018-10-29 22:16:10
# 7 2018-10-29 22:19:10
# 8 2018-10-29 22:25:10
# $`4`
#                   time
# 9  2018-10-29 22:37:10
# 10 2018-10-29 22:50:10

So you could easily do something like:

for (d in split(df, list(findInterval(df$time, bins)))) {
  # d is a data.frame with all data inside a 30-min interval
}
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • This is a little different to what I need to do. What you have done here is binned the data into unique 30 minute intervals. For example 21:00:00 to 21:30:00 and then your next interval is from 21:30:00 to 22:00:00. Whereas what I need is to have 30 minute intervals from 21:00:00 to 21:30:00 and then the next interval from 21:01:00 to 21:31:00 – Seamus O'Leary Oct 30 '18 at 04:38
  • Ok, a moving 30-minute window ... yeah, that's a bit ... err ... different ... have you looked at `zoo::rollapply`? It does windows, though I'm not a pro at it. – r2evans Oct 30 '18 at 04:47