Skip to Content

Convert an irregular to regular precipitation time series

I have an xts series with a year of precipitation data:

str(data_prec)
An ‘xts’ object from 2011-01-01 to 2011-12-31 23:55:00 containing:
  Data: num [1:105125, 1] 0 0 0 0 0 0 0 0 0 0 ...
  Indexed by objects of class: [POSIXct,POSIXt] TZ: 
  xts Attributes:  
List of 2
 $ tclass: chr [1:2] "POSIXct" "POSIXt"
 $ tzone : chr ""

Part of data looks like:

2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0.1
2011-12-15 05:30:00, 1.2
2011-12-15 05:31:00, 0.2
2011-12-15 05:32:00, 0.6
2011-12-15 05:33:00, 0.1
2011-12-15 05:35:00, 0.1
2011-12-15 05:36:00, 0
2011-12-15 05:37:00, 0.6
2011-12-15 05:40:00, 0
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0.1

I need to have my data at each five minutes, by summing the previous data. I've tried to use aggregate, to.minutes5 and merge without success. I don't know what I'm doing wrong. This is the closest way I've reached:

align.time(period.sum(data_prec,endpoints(data_prec,"minutes",k=5)),300)

That gave me:

2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0      
2011-12-15 05:30:00, 0.1    
2011-12-15 05:35:00, 2.1    
2011-12-15 05:40:00, 0.7    
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0      
2011-12-15 05:55:00, 0.1
2011-12-15 06:00:00, 0

This is what I'm looking for:

2011-12-15 05:15:00, 0
2011-12-15 05:20:00, 0
2011-12-15 05:25:00, 0.1
2011-12-15 05:30:00, 1.2
2011-12-15 05:35:00, 1.0
2011-12-15 05:40:00, 0.6
2011-12-15 05:45:00, 0
2011-12-15 05:50:00, 0.1
2011-12-15 05:55:00, 0
2011-12-15 06:00:00, 0

Thanks for any suggestion.