将 xts 或 zoo 时间序列对象的每一行除以固定行 [英] Dividing each row of an xts or zoo time series object by a fixed row

查看:32
本文介绍了将 xts 或 zoo 时间序列对象的每一行除以固定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试划分一个 xts 对象,该对象包含多个时间序列(列;带有一个公共日期列(索引).我想将每列除以其在指定日期的值(比如 '2010-09-30').这是为了在那个日期用每列中的值 1 重新缩放整个对象(一个常见的重新设置任务).如果它是一个普通矩阵,A,我想重新定位的行是 A[6,],我可以做

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,])

这行得通.但是,尝试操作 xts 对象及其行子集 xts['2010-09-30'] 并不容易.有人可以指出我正确的方向.我意识到这是非常基本的,我应该自己找到答案.事实上,如果有一种更好的方法可以使用包以这种方式重新设定时间序列,我很乐意采用这种方法.

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.

推荐答案

xts 和 zoo 对象在操作前按索引对齐.如果您想将整个对象除以一行中的值,您必须使用 coredata(可能还有 drop)将值转换为原子向量(使用只有一个元素).

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).

例如:

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

这篇关于将 xts 或 zoo 时间序列对象的每一行除以固定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆