[R-SIG-Finance] re-indexing data under the zoo package
Aidan Corcoran
aidan.corcoran11 at gmail.com
Mon Feb 28 19:58:13 CET 2011
thanks very much Jeffrey and Stefan, this is just what I wanted. It's
a neat answer (just a pity zoo doesn't provide some utility for this).
cheers
Aidan
On Mon, Feb 28, 2011 at 6:17 PM, Jeffrey Ryan <jeffrey.ryan at lemnica.com> wrote:
> Haven't checked the math per se, but this is probably what you want:
> z <- zooreg(matrix(rnorm(18),nc=3), start=1995, freq=4)
> z[index(z)==1996]
> 1996(1) -1.274160 0.6255354 0.4150378
> t(t(z)/as.numeric(z[index(z)==1996]))
> x.1 x.2 x.3
> 1995(1) 0.9233051 0.2428291171 0.1960831
> 1995(2) 0.6585486 1.2370305821 -0.7346616
> 1995(3) -0.6777132 0.0009999497 -2.6567196
> 1995(4) 0.3662096 1.3125214482 -0.6704602
> 1996(1) 1.0000000 1.0000000000 1.0000000
> 1996(2) -0.2993159 1.9259156026 1.5291669
> The trick is zoo (and xts) merge by time first before doing basic Ops. So
> you are essentially left with only one row if you don't get rid of the
> 'zoo'-ness of one of your objects.
> All the t()'s are just to get things looking like they did at the beginning.
> Best,
> Jeff
>
> On Mon, Feb 28, 2011 at 11:36 AM, Aidan Corcoran
> <aidan.corcoran11 at gmail.com> wrote:
>>
>> Dear all,
>>
>> I was hoping someone could help me to generate an index based on data
>> in zooreg format. The data are of the form
>>
>> > head(dq)
>> sphsxs eunrfi irhont
>> 1995(1) 670.8 82.9 NA
>> 1995(2) 686.0 82.9 NA
>> 1995(3) 682.6 83.0 NA
>> 1995(4) 692.7 82.7 NA
>> 1996(1) 686.0 81.5 33.6
>> 1996(2) 697.8 82.0 34.6
>>
>> and I would like to index each of the three variables to 100 in
>> 1996(1). I have made a few failed attempts based on extracting the
>> values at that date
>>
>> > dq[index(dq)==1996.00]
>> sphsxs eunrfi irhont
>> 1996(1) 686 81.5 33.6
>>
>> and then trying to divide the series by those values
>>
>> > dq/dq[index(dq)==1996.00]
>> sphsxs eunrfi irhont
>> 1996 1 1 1
>>
>> but this results in a single row. One option might be to replicate the
>> 1996 row using rep, but
>>
>> > rep(dq[index(dq)==1996.00],2)
>> [1] 686.0 81.5 33.6 686.0 81.5 33.6
>>
>> seems to repeat the data within a single vector, and I'm not sure how
>> to get it to repeat the row down through a zoo object (and suspect
>> there might be an easier way).
>>
>> Any help much appreciated.
>>
>> thanks
>> Aidan
>>
>> _______________________________________________
>> R-SIG-Finance at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions
>> should go.
>
>
>
> --
> Jeffrey Ryan
> jeffrey.ryan at lemnica.com
>
> www.lemnica.com
>
>
More information about the R-SIG-Finance
mailing list