Look at your function; it is returning exactly what you are asking for:

x.dif <- c(diff(my.vec), diff(my.vec, lag=i))  # the first and last values

You probably want something like this:

dif <- function(my.vec) {
x.diff <- diff(my.vec)
for(i in 2:(length(my.vec)-1)) {
x.dif <- c(x.diff, diff(my.vec, lag=i))
}
return(x.dif)
}

You might also want to check if the length of the vector is 2, or less,
since your 'for' will not work.
On Fri, Jan 1, 2010 at 8:16 PM, Rafael Moral <rafa_moral2004@yahoo.com.br>wrote:

> Dear useRs,
>
> I want to write a function that generates all the possible combinations of
> diff().
>
> Example:
> If my vector has length 5, I need the diff() until lag=4 ->
> c(diff(my.vec), diff(my.vec, lag=2), diff(my.vec, lag=3), diff(my.vec,
> lag=4))
>
> If it has length 4, I need until lag=3 ->
> c(diff(my.vec), diff(my.vec, lag=2), diff(my.vec, lag=3))
>
> So, it must be until lag=(length(my.vec)-1).
>
> The function I've written is:
> dif <- function(my.vec) {
> for(i in 2:(length(my.vec)-1)) {
> x.dif <- c(diff(my.vec), diff(my.vec, lag=i))
> }
> return(x.dif)
> }
>
> But it only returns the first diff() (lag=1) and the last one (
> diff(my.vec, lag=(length(my.vec)-1)   )
> Example:
> > my.vec = c(1,2,3,2)
> > dif(my.vec)
> [1]  1  1 -1  1
> What I wanted to get was:
> > c(diff(my.vec), diff(my.vec, lag=2), diff(my.vec, lag=3))
> [1]  1  1 -1  2  0  1
>
> Is there a way of computing it so R understands what I want?
>
> Thanks in advance, happy new year for everyone!
>
> Kind regards,
> Rafael.
>
>
>
>  ____________________________________________________________________________________
> [[elided Yahoo spam]]
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

	[[alternative HTML version deleted]]

