문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top