Convert hourly rain data daily at a specific time interval

I have hourly data on precipitation and temperature over a long period. I would like to get daily values ​​from hourly data. I consider daily funds from 07:00:00 until the next day 07:00:00.

Could you tell me how to convert hourly data per day between a specific time interval?

example: 07:00:00 to 07:00:00 or 12:00:00 to 12:00:00 )

Precipitation data is as follows:

 1970-01-05 00:00:00 1.0 1970-01-05 01:00:00 1.0 1970-01-05 02:00:00 1.0 1970-01-05 03:00:00 1.0 1970-01-05 04:00:00 1.0 1970-01-05 05:00:00 3.6 1970-01-05 06:00:00 3.6 1970-01-05 07:00:00 2.2 1970-01-05 08:00:00 2.2 1970-01-05 09:00:00 2.2 1970-01-05 10:00:00 2.2 1970-01-05 11:00:00 2.2 1970-01-05 12:00:00 2.2 1970-01-05 13:00:00 2.2 1970-01-05 14:00:00 2.2 1970-01-05 15:00:00 2.2 1970-01-05 16:00:00 0.0 1970-01-05 17:00:00 0.0 1970-01-05 18:00:00 0.0 1970-01-05 19:00:00 0.0 1970-01-05 20:00:00 0.0 1970-01-05 21:00:00 0.0 1970-01-05 22:00:00 0.0 1970-01-05 23:00:00 0.0 1970-01-06 00:00:00 0.0 
+4
source share
4 answers

You can use this code:

 fun <- function(s,i,j) { sum(s[i:(i+j-1)]) } sapply(X=seq(1,24*nb_of_days,24),FUN=fun,s=your_time_serie,j=24) 

You just need to change 1 to a different value for a different time interval: 8 from 07:00:00 to 07:00:00 or 13 from 12:00:00 to 12:00:00

+2
source

Create some reproducible data first so that we can help you better:

 require(xts) set.seed(1) X = data.frame(When = as.Date(seq(from = ISOdatetime(2012, 01, 01, 00, 00, 00), length.out = 100, by="1 hour")), Measurements = sample(1:20, 100, replace=TRUE)) 

Now we have a data frame with 100 hourly observations, where the dates start from 2012-01-01 00:00:00 and end at 2012-01-05 03:00:00 (the time is in a 24-hour format).

Secondly, convert it to an XTS object.

 X2 = xts(X$Measurements, order.by=X$When) 

Third, learn how to subset a specific time window.

 X2['T04:00/T08:00'] # [,1] # 2012-01-01 04:00:00 5 # 2012-01-01 05:00:00 18 # 2012-01-01 06:00:00 19 # 2012-01-01 07:00:00 14 # 2012-01-01 08:00:00 13 # 2012-01-02 04:00:00 18 # 2012-01-02 05:00:00 7 # 2012-01-02 06:00:00 10 # 2012-01-02 07:00:00 12 # 2012-01-02 08:00:00 10 # 2012-01-03 04:00:00 9 # 2012-01-03 05:00:00 5 # 2012-01-03 06:00:00 2 # 2012-01-03 07:00:00 2 # 2012-01-03 08:00:00 7 # 2012-01-04 04:00:00 18 # 2012-01-04 05:00:00 8 # 2012-01-04 06:00:00 16 # 2012-01-04 07:00:00 20 # 2012-01-04 08:00:00 9 

Fourth, use this information with apply.daily and any function you want:

 apply.daily(X2['T04:00/T08:00'], mean) # [,1] # 2012-01-01 08:00:00 13.8 # 2012-01-02 08:00:00 11.4 # 2012-01-03 08:00:00 5.0 # 2012-01-04 08:00:00 14.2 

Update: custom endpoints

After reading your question again, I see that I misinterpreted what you wanted.

It seems that you want to accept the value of a 24-hour period, not necessarily from midnight to midnight.

To do this, you need to take apply.daily and use period.apply with custom endpoint s instead, for example:

 # You want to start at 7AM. Find out which record is the first one at 7AM. A = which(as.character(index(X2)) == "2012-01-01 07:00:00") # Use that to create your endpoints. # The ends of the endpoints should start at 0 # and end at the max number of records. ep = c(0, seq(A, 100, by=24), 100) period.apply(X2, INDEX=ep, FUN=function(x) mean(x)) # [,1] # 2012-01-01 07:00:00 12.62500 # 2012-01-02 07:00:00 10.08333 # 2012-01-03 07:00:00 10.79167 # 2012-01-04 07:00:00 11.54167 # 2012-01-05 03:00:00 10.25000 
+4
source

Step 1: Convert Date to POSIXct

 ttt <- as.POSIXct("1970-01-05 08:00:00",tz="GMT") ttt #"1970-01-05 08:00:00 GMT" 

Step 2: Subtract the difference of 7 hours

 ttt <- ttt-as.difftime(7,units="hours") ttt #"1970-01-05 01:00:00 GMT" 

Step 3: trunc to days

 ttt<-trunc(ttt,"days") ttt #"1970-01-05 GMT" 

Step 4: use plyr, data.table or any other method you prefer to calculate daily funds

+1
source

Using regular expressions , you get what you need. Choose the rows that fit your needs and summarize the values. Do this for each day within your time zone and you will set.

0
source

All Articles