Skip to content

help with for loop

4 messages · Rafael Moral, jim holtman, Gabor Grothendieck +1 more

#
diff.zoo in the zoo package can take a vector of lags:

library(zoo)
z <- zoo(seq(10)^2)
diff(z, 1:4)

There are three vignettes (pdf documents) that come with zoo that have
more info on the package.


On Fri, Jan 1, 2010 at 8:16 PM, Rafael Moral
<rafa_moral2004 at yahoo.com.br> wrote:
#
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-bounces at r-project.org [mailto:r-help-bounces at 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_moral2004 at yahoo.com.br>wrote:

            
____________________________________________________________________________
________
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting
-guide.html>