[R] help with for loop

Bert Gunter gunter.berton at gene.com
Sat Jan 2 18:40:02 CET 2010


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:

> 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 at 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 at 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.



More information about the R-help mailing list