Reputation: 195
I have a column or vector of times. The data were originally evenly spaced at 2 min intervals but I subsetted the data to look at records of interest. I now want to group all time records together that are consecutively 2 mins apart. The result would be a column/vector called 'TimeGroup' to identify which records are grouped together.
DateTime<-as.POSIXct( strptime(c("2013-05-27 12:41:52 UTC", "2013-05-27 13:25:52 UTC", "2013-05-27 13:27:52 UTC",
"2013-05-27 13:29:52 UTC" ,"2013-05-27 13:31:52 UTC" ,"2013-05-27 13:33:52 UTC",
"2013-05-27 13:35:52 UTC" ,"2013-05-27 13:37:52 UTC" ,"2013-05-27 13:39:52 UTC",
"2013-05-27 13:41:52 UTC", "2013-05-27 13:43:52 UTC", "2013-05-27 13:45:52 UTC",
"2013-05-27 13:47:52 UTC" ,"2013-05-27 13:49:52 UTC" ,"2013-05-27 13:51:52 UTC",
"2013-05-27 13:53:52 UTC", "2013-05-27 13:55:52 UTC", "2013-05-27 13:57:52 UTC",
"2013-05-27 13:59:52 UTC", "2013-05-27 14:01:52 UTC" ,"2013-05-27 14:03:52 UTC",
"2013-05-27 14:05:52 UTC", "2013-05-27 14:07:52 UTC", "2013-05-27 14:41:52 UTC",
"2013-05-27 14:43:52 UTC" ,"2013-05-27 14:45:52 UTC", "2013-05-27 14:47:52 UTC",
"2013-05-27 15:11:52 UTC" ,"2013-05-27 15:13:52 UTC", "2013-05-27 15:15:52 UTC",
"2013-05-27 15:17:52 UTC" ,"2013-05-27 15:21:52 UTC", "2013-05-27 15:23:52 UTC",
"2013-05-27 15:25:52 UTC" ,"2013-05-27 15:27:52 UTC", "2013-05-27 15:29:52 UTC",
"2013-05-27 15:33:52 UTC" ,"2013-05-27 15:35:52 UTC" ,"2013-05-27 15:37:52 UTC",
"2013-05-27 15:39:52 UTC", "2013-05-27 15:41:52 UTC" ,"2013-05-27 15:43:52 UTC",
"2013-05-27 15:45:52 UTC" ,"2013-05-27 15:47:52 UTC" ,"2013-05-27 16:03:52 UTC",
"2013-05-27 16:05:52 UTC", "2013-05-27 16:07:52 UTC", "2013-05-27 16:09:52 UTC",
"2013-05-27 16:11:52 UTC", "2013-05-27 16:13:52 UTC", "2013-05-27 16:15:52 UTC",
"2013-05-27 16:17:52 UTC" ,"2013-05-27 16:19:52 UTC" ,"2013-05-27 16:21:52 UTC",
"2013-05-27 16:23:52 UTC", "2013-05-27 16:25:52 UTC" ,"2013-05-27 16:27:52 UTC",
"2013-05-27 16:29:52 UTC" ,"2013-05-27 16:31:52 UTC" ,"2013-05-27 16:33:52 UTC",
"2013-05-27 16:39:52 UTC" ,"2013-05-27 16:41:52 UTC" ,"2013-05-27 16:43:52 UTC",
"2013-05-27 16:45:52 UTC" ,"2013-05-27 16:47:52 UTC" ,"2013-05-27 16:49:52 UTC",
"2013-05-27 16:51:52 UTC" ,"2013-05-27 16:53:52 UTC" ,"2013-05-27 16:55:52 UTC",
"2013-05-27 16:57:52 UTC" ,"2013-05-27 16:59:52 UTC" ,"2013-05-27 17:01:52 UTC",
"2013-05-27 17:03:52 UTC" ,"2013-05-27 17:05:52 UTC" ,"2013-05-27 17:07:52 UTC",
"2013-05-27 17:09:52 UTC", "2013-05-27 17:11:52 UTC" ,"2013-05-27 17:13:52 UTC",
"2013-05-27 17:15:52 UTC", "2013-05-27 17:17:52 UTC" ,"2013-05-27 17:19:52 UTC"), format="%Y-%m-%d %H:%M:%S"),1970-01-01, tz="UTC")
diff(DateTime)
Time differences in mins
[1] 44 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 34 2 2 2 24 2 2 2 4 2 2 2
[35] 2 4 2 2 2 2 2 2 2 16 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 6 2 2 2 2 2 2 2 2
[69] 2 2 2 2 2 2 2 2 2 2 2 2
From this example data, there would be 7 TimeGroups called 1,2,3,4,5,6,7. The records not apart of a time group would be NA.
Upvotes: 0
Views: 32
Reputation: 27782
try the following
library( data.table )
DT <- data.table( DateTime = DateTime )
DT[, group := c(0,cumsum( diff(DateTime) > 2)) ]
Upvotes: 1