Reputation: 18580
I have a multicolumn xts that I would like to divide by one xts (using the date as primary key, of course).
Is there a way to do that in a vectorized manner?
Thanks
Upvotes: 0
Views: 1676
Reputation: 174813
See ?sweep
(if I understand what you meant - reproducible example!?):
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, "/")
Which gives:
> 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
Upvotes: 8