开发者

R: Aggregating by dates with POSIXct?

I have some zoo series that use POSIXct index.

In order to aggregate by days I've tried these two ways:

aggregate(myzoo,format((index((myzoo)),"%Y-%m-%d")),sum)

aggregate(myzoo,as.Date(index(myzoo)开发者_如何学Go),sum)

I don't know why they don't give the same output. myzoo series had the weekends removed. The "as.Date way" seems to be OK but the "format way" aggregation gives me data on the weekends.

Why?

Which one is the right?

I've even tried it as.POSIXct(format(...))


As I mentioned in my comment, you need to be careful when changing the format of a timestamp that includes time with a time zone, because it can get shifted between days. Without any data, it's hard to say exactly what your problem is, but you might also try apply.daily from xts:

apply.daily(myzoo, sum)

Here's a working example:

> x <-  zoo(2:20, as.POSIXct("2003-02-01") + (2:20) * 7200)
> apply.daily(x, sum)
> 2003-02-01 22:00:00 2003-02-02 16:00:00 
                 65                 144 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜