R中的平均值和sd与xts

时间:2013-05-13 12:05:51

标签: r xts zoo mean standard-deviation

再次,我确实在xts中有我的df并且没有名字! (据我所知,设置为.POSIXct())时不再有名字了:

    "2012-04-09 05:00:00",2
    "2012-04-09 09:00:00",4
    "2012-04-09 12:00:00",5
    "2012-04-09 22:00:00",0
    "2012-04-10 04:00:00",0
    "2012-04-10 06:00:00",3
    "2012-04-10 08:00:00",0
    "2012-04-10 12:00:00",1

我想计算当天的平均值和sd - 而不是整个df。

df2<-period.apply(df, endpoints(df, "hours", 24), mean)

有效,但我不是有一天的意思 - 以及如何处理标准偏差? 感谢

2 个答案:

答案 0 :(得分:2)

apply.daily能做你想做的吗?

> apply.daily(df, mean)
                    [,1]
2012-04-09 22:00:00 2.75
2012-04-10 12:00:00 1.00
> apply.daily(df, sd)
                        [,1]
2012-04-09 22:00:00 2.217356
2012-04-10 12:00:00 1.414214

答案 1 :(得分:1)

by(value,as.Date(df$timestamp),mean)
by(value,as.Date(df$timestamp),sd)