XTS 应用系列和多列 XTS? [英] XTS Apply Family and a Multi Column XTS?
问题描述
我如何使用 apply 系列函数,比如 apply.daily
到多变量 XTS?
例如:
<前>时间,a,b...2012-02-11 16:21:24 4.7258 7.72582012-02-11 16:26:25 4.9096 12.37962012-02-11 16:31:25 4.7904 2.2204...我将如何按列将 apply.daily
和 mean
用于整个矩阵.因此,结果将是当天的单个时间戳,下一列的 a
的平均值,以及之后列的 b
的平均值.
我想对任意数量的列执行此操作(列和名称的数量未知——当然都是数字).
你可以简单地使用 colMeans
来取每一列的平均值:
库(quantmod)getSymbols("间谍")spy1 <- apply.weekly(SPY, colMeans)
您还可以定义一个任意函数,该函数在对象的列上使用 apply
:
spy2 <- apply.weekly(SPY, function(x) apply(x,2,mean))相同(间谍1,间谍2)# [1] 真
How do I use the apply family of functions, say apply.daily
to a multivariate XTS?
So for example:
Time,a,b ... 2012-02-11 16:21:24 4.7258 7.7258 2012-02-11 16:26:25 4.9096 12.3796 2012-02-11 16:31:25 4.7904 2.2204 ...
How would I use apply.daily
and mean
to the entire matrix by column. So the result would be a single time stamp for the day, the mean of a
for the next column, and the mean of b
for the column after that.
I would like to do this for arbitrary number columns (the amount of columns and names are not known -- all numeric of course).
You could simply use colMeans
to take the mean of every column:
library(quantmod)
getSymbols("SPY")
spy1 <- apply.weekly(SPY, colMeans)
You could also define an arbitrary function that uses apply
over the columns of your object:
spy2 <- apply.weekly(SPY, function(x) apply(x,2,mean))
identical(spy1,spy2)
# [1] TRUE
这篇关于XTS 应用系列和多列 XTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!