0

I have data of the following form:

DateTime          |     Var1
11/01/2016 06:01  |       0 
11/01/2016 06:02  |    0.70 
...
...
11/01/2016 23:59  |   35.08
11/02/2016 00:01  |   33.29
...
11/02/2016 06:00  |   24.62
...
11/30/2016 23:59  |   42.08
12/01/2016 00:01  |   39.79
....

I have ~5 months data. I have to subset the data from 6:00am of 1 day to just before 6:00am of next day. I can use the following code to subset the data once I have the dates in hand, but how to automatically obtain all the successive dates from the input data?

Date1 <- as.integer(as.POSIXct(Date1)) 
Date2 <- as.integer(as.POSIXct(Date2))
subset <- subset(data, as.integer(as.POSIXct(data$txtime)) >= Date1 & as.integer(as.POSIXct(data$txtime)) < Date2)

Right now, I can use to following code to obtain successive dates within a month, but this won't work for the last day of the month, where part of the data to be subsetted is on the first day of the next month. So I can't do it automatically for the duration 6:00am 30th November - 5:59am 1st December. Also, the code is not fully automated, as the number of days (used in the loop) varies across months.

for (dateofmonth in c(1:29)) {
Date1 <- paste("2016-11-", dateofmonth, ' 06:00:00', sep = '')
Date2 <- paste("2016-11-", (dateofmonth+1), ' 06:00:00', sep = '')
}

There is possibly an easier way to do this, but I can't figure it out. Please suggest.

Sree
  • 77
  • 1
  • 8

1 Answers1

0

Try this:

datelist <- split(data, as.Date(as.POSIXct(data$txtime)-21600))

This will shift your time 6 hours backwards, and then split your data by date. So that each sub dataframe will contain times from 6:00 am in that date to 5:59 am in next day.

Feng
  • 603
  • 3
  • 9
  • Is there a way to subset it into dataframes? I want to run calculations on the data after subsetting, and a dataframe may be more useful. – Sree Mar 24 '17 at 10:38
  • Each element of the result `datelist` is a subset dataframe. You can run calculations to specific subsets or apply to all subsets by using `lapply`. – Feng Mar 26 '17 at 07:04