【问题标题】:mean and sd of the day in R with xts带有xts的R中当天的平均值和标准差
【发布时间】:2013-05-07 11:06:17
【问题描述】:

我再次在 xts 中有我的 df 并且没有名字! (据我所知,设置 as.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

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

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

有效,但我得到的不是一天的平均值 - 以及如何处理标准偏差? 谢谢

【问题讨论】:

  • 如果你想要“天”,你可以这样做,而不是 24“小时”。 period.apply(df, endpoints(df, "days"), mean)。标准差相同:period.apply(df, endpoints(df, "days"), sd)

标签: r xts zoo mean standard-deviation


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    by(value,as.Date(df$timestamp),mean)
    by(value,as.Date(df$timestamp),sd)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 2016-03-11
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多