Check out ?diff and ?lag.  Also, it's kind of a personal preference, but
xts is a pretty attractive alternative to timeSeries that many of us use.

library(xts)
set.seed(10)
x <- xts(rnorm(10), Sys.Date()+1:10)
diff(x)
x - lag(x)



On Fri, Jun 20, 2014 at 8:14 AM, nacho <ignacio.rodrigo@gmx.com> wrote:

> Hi guys,
>
> First of all, apologies for the simplicity of the question, I am a complete
> R and Rmetrics newbie so I am just trying to learn. I have searched forums
> and the internet in general for the answer but I have found none that I
> could understand or matched my query.
>
> So, using the Rmetrics package, I created a timeSeries object with a set of
> closing prices, e.g.
>
> > data
>
> GMT
>                 Close
> 1974-12-31  191
> 1975-01-02  184
> 1975-01-03  173
> 1975-01-06  172
> 1975-01-07  171
> 1975-01-08  176
> 1975-01-09  179
> 1975-01-10  177
> 1975-01-13  181
> 1975-01-14  177
>
> My first question is how to apply a transformation of one row relative to
> the previous row to the 'data' timeSeries object and store it in a new
> timeSeries object. I am aware that you can simply run:
>
> > returns(data)
>
> ...and it will generate a returns timeSeries object. However, what if I
> want
> to create a new timeSeries object from 'data' that applies a more custom
> transformation that references other rows? e.g. one that subtracts the
> previous observation from the current one
>
> GMT
>                 Close
> 1975-01-02  -7
> 1975-01-03  -11
> 1975-01-06  -1
> 1975-01-07  -1
> 1975-01-08  5
> 1975-01-09  3
> 1975-01-10  -2
> 1975-01-13  4
> 1975-01-14  -4
>
> I have tried doing it writing a for-loop but something tells me that it is
> possible to do it in a much simpler way through timeSeries' fapply() or
> applySeries() functions. Would anyone know how to do it?
>
> Thanks a lot for taking the time to read this and for your help in advance,
> much appreciated!
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Applying-transformations-to-timeSeries-objects-tp4692438.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance@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.
>

	[[alternative HTML version deleted]]

