If I understand the query, z <- outer(x,x,"-") z[lower.tri(z)] ## is what you want.
?outer and ?lower.tri will tell you how to interpret what you get. Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of jim holtman Sent: Friday, January 01, 2010 5:25 PM To: Rafael Moral Cc: r-help Subject: Re: [R] help with for loop 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_moral2...@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]] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code. ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.