Question

I want to aggregate the data by time and create equally spaced time intervals:

date<- c(as.POSIXct("2011-08-08 21:00:00"), as.POSIXct("2011-08-08 21:26:00"))
value<-c(1,2)
dt<-data.frame(date, value)

DT<-aggregate(cbind(dt$value),list(cut(dt$date, breaks="10 min")),sum) 

dt:
2011-08-08 21:00:00 1
2011-08-08 21:26:00 2

DT:
2011-08-08 21:00:00 1
2011-08-08 21:20:00 2

What I want:

2011-08-08 21:00:00 1
2011-08-08 21:10:00 NA
2011-08-08 21:20:00 2

Is there anyway to do this without using zoo or xts?

Was it helpful?

Solution

I'm assuming by your last line that you are trying to avoid using packages.

Sticking with base R, you can try tapply, but your dates will become the rownames (or names, if you skip the data.frame step):

data.frame(value = tapply(cbind(dt$value),
                          list(cut(dt$date, breaks="10 min")),
                          sum))
#                     value
# 2011-08-08 21:00:00     1
# 2011-08-08 21:10:00    NA
# 2011-08-08 21:20:00     2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top