简体   繁体   English

XTS应用系列和多列XTS?

[英]XTS Apply Family and a Multi Column XTS?

How do I use the apply family of functions, say apply.daily to a multivariate XTS? 如何使用apply系列函数,例如apply.daily到多变量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. 我如何使用apply.daily并按列对整个矩阵mean 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. 因此,其结果将是一天一个时间戳,平均a对下一列,并且平均b的后列。

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: 您可以简单地使用colMeans来获取每列的平均值:

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: 您还可以定义一个任意函数,该函数使用apply对象的列:

spy2 <- apply.weekly(SPY, function(x) apply(x,2,mean))
identical(spy1,spy2)
# [1] TRUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM