R timestamp with iteration for Machine Learning- 3

2019-09-14 02:25发布

问题:

I have a data frame with a timestamp column (converted to real time) that looks like this (only a small portion is shown):

"2016-05-25 08:59:21 EDT" 
"2016-05-25 08:59:21 EDT" 
"2016-05-25 08:59:21 EDT"
"2016-05-25 08:59:22 EDT"
"2016-05-25 09:39:57 EDT"
"2016-05-25 09:40:33 EDT"

(Of course, there are other columns that are adjacent to the timestamp column.)

What I want to achieve is...to divide all the data into 30 minute sections. So, I want all the data between 08:59:00 ~ 09:29:00 grouped together, 09:30~09:59 grouped together and so on.

I was thinking:

library(dplyr)
data %>%
    filter(timestamp > 2016-05-25 08:59:00 & timestamp < 2016-05-25 09:29:00) 

However, doing this manually would take forever. The data has thousands of rows. I want to use a for loop. Something along the lines of:

for (i in 1:nrow(data)) {
   for (j in 1:nrow(data)) {
      if (data$timestamp[i+j] - data$timestamp[i] == 30) {
         data <- data[i:(i+j), ] 
    }
}

Any feedback is appreciated.