R timestamp with iteration for Machine Learning- 30 minute window

馋奶兔 提交于 2020-01-07 03:49:35

问题


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.

来源:https://stackoverflow.com/questions/45048231/r-timestamp-with-iteration-for-machine-learning-30-minute-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!