Re: [R] Pointwise division of two zoo objects?

From: Kenn Konstabel <>
Date: Tue, 15 Mar 2011 11:02:50 +0200


> returns.z = tail(prices.z,-1)/head(prices.z,-1) - 1 # should be equivalent
> to returns = exp(diff(log(prices.z))) - 1
> Curiously, I get a zoo object back with zeros everywhere and also with the
> index having one fewer element than it should.
> Does anyone know how to pointwise divide zoo objects, and what exactly "/"
> is doing?

I think any arithmetic operations on zoo objects work on values having the same indexes. So if

x <- zoo(1:10, 1:10)
y <- zoo(1:10, 11:20)
# no result as no elements have the same index y <- zoo(1:10, 6:15)
# last five elements from x, first five from z

To do your first example, you can use coredata so that one of your obejcts would be an ordinary vector:

returns.z = tail(prices.z,-1)/coredata(head(prices.z,-1)) - 1



        [[alternative HTML version deleted]] mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 15 Mar 2011 - 09:06:24 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 15 Mar 2011 - 11:10:21 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive