1

again I do have my df in xts and don't have names! (as far as I know there is no name anymore when setting 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

I wanna calculate the mean and sd of the day - not of the whole df.

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

works but I am getting not the mean of one day - and how to deal with the standard deviation? Thanks

Herr Student
  • 853
  • 14
  • 26
  • 1
    If you want "days", you can just do that instead of 24 "hours". `period.apply(df, endpoints(df, "days"), mean)`. Standard deviation is the same: `period.apply(df, endpoints(df, "days"), sd)` – GSee May 13 '13 at 14:58

2 Answers2

2

Does apply.daily do what you want?

> 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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
1
by(value,as.Date(df$timestamp),mean)
by(value,as.Date(df$timestamp),sd)
Thomas
  • 43,637
  • 12
  • 109
  • 140