【发布时间】:2018-06-20 16:33:47
【问题描述】:
我想在整个时间序列中量化每年高于恒定值(例如 > 0.005)的天数。
我用这个例子来解释我的问题:
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))))
要计算年平均值没关系,我使用函数“长度”来估计它的工作年数,但是如果我添加一个条件 长度(其中(x> 0.005))))它不起作用!
你有没有想法如何在 period.apply 中使用条件来改进长度函数!
提前感谢您的帮助
【问题讨论】:
标签: r function statistics apply xts