Kyle Brandt
Kyle Brandt

Reputation: 28457

XTS apply function to time of day subset?

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?

Upvotes: 3

Views: 1542

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176738

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.

Upvotes: 3

Related Questions