将 xts 或 Zoo 时间序列对象的每一行除以固定行

发布于 2024-12-22 14:53:56 字数 350 浏览 1 评论 0原文

我正在尝试划分一个包含多个时间序列(列;具有公共日期列(索引))的 xts 对象。我想将每列除以其在指定日期的值(例如“2010-09-30”)这是为了在该日期将整个对象的每列值重新调整为 1(如果它是普通矩阵 A 和行)。我想重新设定为就是说 A[6,],我可以这样做

t(t(A)/A[6,])

并且可行,但是,尝试操作 xts 对象及其行子集 xts['2010-09-30'] 并不那么容易工作。有人可以指出我正确的方向吗?我意识到这是非常基本的,事实上,如果有更好的方法来调整时间。系列以这种方式使用一个包,我我很高兴采用这种方法。

I am trying to divide an xts object which holds a number of time series (columns; with a common date column (index). I want to divide each column by its value at a specified date (say '2010-09-30'). This is so as to re-scale the entire object with values of 1 in each column at that date (a common re-basing task). Had it been an ordinary matrix, A, and the row I wanted to rebase to was say A[6,], I could just do

t(t(A)/A[6,])

and that works. But, trying to manipulate the xts object and its row subset xts['2010-09-30'] doesn't work as easily. Could someone please point me in the right direction. I realise this is very basic and I should have found the answer on my own. In fact, if there is a better method in general for rebasing time series in this manner using a package, I am happy to adopt that approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-12-29 14:53:56

xts 和 Zoo 对象在操作之前按索引对齐。如果您想将整个对象除以单行的值,则必须使用 coredata(也可能是 drop)将值获取到原子向量(使用只有一个元素)。

例如:

library(xts)
x <- xts(1:10,as.Date("2011-12-21")+1:10)
x / drop(coredata(x['2011-12-26']))

xts and zoo objects are aligned by index before operations. If you want to divide an entire object by a value at a single row, you have to use coredata (and maybe drop) to get the value to an atomic vector (with only one element).

For example:

library(xts)
x <- xts(1:10,as.Date("2011-12-21")+1:10)
x / drop(coredata(x['2011-12-26']))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文