3

How can I apply a summary function to a time of day subset?

For example with:

r['T16:00/T17:00']$Value

How can I apply something like function (x) quantile(x, c(.90)) for Value over each day's sample hour?

Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165

1 Answers1

3

You can use apply.daily to apply a function to each day's data after you've done the time-of-day subset.

rt <- r['T16:00/T17:00','Value']
rd <- apply.daily(rt, function(x) xts(t(quantile(x,0.9)), end(x)))

You can see I needed to do a few backflips to ensure the object returned from your function can be handled by apply.daily. Mainly, it has to be a multi-column xts object with one row.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • `plot(apply.daily(rt, function(x) quantile(x, c(.90))))` Works ... is there a caveat with that? -- Oh I see, typo in my question so you were going off of that I think – Kyle Brandt Jan 31 '12 at 22:23
  • @KyleBrandt: You're correct. I thought it was a bit odd, but I assumed you did it on purpose. :) I'll update my answer. – Joshua Ulrich Jan 31 '12 at 22:55