简体   繁体   中英

divide a matrix by one column

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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