Reputation: 167
I want to quantify the number of days above a constant value (eg. > 0.005) for each year throughout my time series.
I use this example to explain my problematic:
library(xts)
library(PerformanceAnalytics)
data(edhec)
head(edhec)
edhec_4yr <- edhec["1997/2001"]
ep <- endpoints(edhec_4yr, "months")
# mean
period.apply(edhec_4yr, INDEX = ep, function(x) apply(x,2,mean))
# Length
period.apply(edhec_4yr, INDEX = ep, function(x) apply(x,2, length))
# Length with condition (error!!)
period.apply(edhec_4yr, INDEX = ep, function(x) apply(x,2,
length(which(x>0.005))))
To calculate the annual average it's ok, I use the function "Length" to estimate the annual number it works, But if I add a condition length (which (x> 0.005)))) it does not work!
Do you have ideas how to improve the length function with condition in period.apply!
Thank you in advance for your help
Upvotes: 0
Views: 779
Reputation: 26823
You have to supply a function as third argument to apply
, e.g. like this:
period.apply(edhec_4yr,
INDEX = ep,
function(x) apply(x,
2,
function(y) length(which(y>0.005))))
Upvotes: 1