2011-10-25 45 views
0

我有一個多列xts,我想除以一個xts(使用日期作爲主鍵,當然)。除以一列的矩陣

有沒有辦法以矢量化的方式做到這一點?

感謝

回答

8

?sweep(如果我明白你的意思 - 重複的例子!?):

data(sample_matrix) 
sample.xts <- as.xts(sample_matrix, descr='my new xts object') 
## create a matrix... 
m <- sample.xts[, -1] 
## ...and a vector from the sample.xts object 
v <- sample.xts[, 1] 
## apply sweep 
out <- sweep(m, 1, v, "/") 

其中給出:

> class(out) 
[1] "xts" "zoo" 
> head(out) 
       High  Low  Close 
2007-01-02 1.001559 0.9982141 1.0015587 
2007-01-03 1.003810 1.0000000 1.0033281 
2007-01-04 1.000000 0.9968898 0.9982428 
2007-01-05 1.000000 0.9969739 0.9992283 
2007-01-06 1.000000 0.9973506 0.9987421 
2007-01-07 1.001666 0.9972022 0.9972022