Re: [R] Generic 'diff'

2009-05-19 Thread Gabor Grothendieck
Note that this could be done like this for ordinary vectors: > x <- seq(1:4)^2 > apply(embed(x, 2), 1, function(x, f) f(rev(x)), f = diff) [1] 3 5 7 > apply(embed(x, 2), 1, function(x, f) f(rev(x)), f = sum) [1] 5 13 25 or a method to rollapply in zoo could be added for ordinary vectors. Here i

Re: [R] Generic 'diff'

2009-05-19 Thread Wacek Kusnierczyk
Wacek Kusnierczyk wrote: > > btw., the error message here is confusing: > > lag = 1:2 > diff(1:10, lag=lag) > # Error in diff.default(1:10, lag = lag) : > # 'lag' and 'differences' must be integers >= 1 > > is.integer(lag) > # TRUE > all(lag >= 1) > # TRUE > > wh

Re: [R] Generic 'diff'

2009-05-19 Thread Wacek Kusnierczyk
Wacek Kusnierczyk wrote: > Stavros Macrakis wrote: > >> [...] >> I am not talking about creating a new class with an analogue to the >> subtraction function. I am talking about a function which applies another >> function to a sequence and its lagged version. >> >> Functional arguments are use

Re: [R] Generic 'diff'

2009-05-19 Thread Wacek Kusnierczyk
Stavros Macrakis wrote: > On Mon, May 18, 2009 at 6:00 PM, Gabor Grothendieck >> wrote: >> > > >> I understood what you were asking but R is an oo language so >> that's the model to use to do this sort of thing. >> >> > > I am not talking about creating a new class with an analogue

Re: [R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
On Mon, May 18, 2009 at 6:00 PM, Gabor Grothendieck wrote: > I understood what you were asking but R is an oo language so > that's the model to use to do this sort of thing. > I am not talking about creating a new class with an analogue to the subtraction function. I am talking about a function

Re: [R] Generic 'diff'

2009-05-18 Thread Gabor Grothendieck
I understood what you were asking but R is an oo language so that's the model to use to do this sort of thing. On Mon, May 18, 2009 at 5:48 PM, Stavros Macrakis wrote: > I guess I wasn't very clear.  The goal is not to define diff on a different > object type, but to have a different 'subtraction

Re: [R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
I guess I wasn't very clear. The goal is not to define diff on a different object type, but to have a different 'subtraction' operator with the same lag logic. An easy example would be quotient instead of subtraction. Of course I could do that by simply cutting and pasting diff.default and replac

Re: [R] Generic 'diff'

2009-05-18 Thread Gabor Grothendieck
You can define a new class for the object diff operates on and then define your own diff method for that. For some examples see: methods(diff) On Mon, May 18, 2009 at 4:24 PM, Stavros Macrakis wrote: > I would like to apply a function 'f' to the lagged version of a vector and > the vector itse

[R] Generic 'diff'

2009-05-18 Thread Stavros Macrakis
I would like to apply a function 'f' to the lagged version of a vector and the vector itself. This is easy to do explicitly: mapply( f, v[-1], v[-length(v)] ) or in the case of a pointwise vector function, simply f( v[-1], v[-length(v)] ) This is essentially the same as 'diff' but