[R] Iterate over 1 <= i < j <= n
David Meyer
meyer at ci.tuwien.ac.at
Sun Jan 27 23:58:30 CET 2002
On Sun, 27 Jan 2002, Gabor Grothendieck wrote:
>
> What is the preferred way to iterate over 1 <= i < j <= n ?
>
> The following does not work as it gives an error message
> when i+1 exceeds n:
>
> for(i in 1:n)
> for(j in seq(i+1,n,1))
for(i in 1:(n-1))
for (j in (i+1):n)
should work, I think.
-d
>
> This works but seems like too much work:
>
> for(i in 1:n) {
> j <- i+1
> while(j<=n) {
> ...
> j <- j+1
> }
> }
>
> This is briefer but offends my sense of how to do things:
>
> for(i in 1:n)
> for(j in i:n) {
> if (i==j) next
> ...
> }
>
> This works too but seems somewhat involved:
>
> d <- diag(n)
> for( idx in seq(d)[upper.tri(d,diag=FALSE)] ) {
> i <- row(d)[idx]; j <- col(d)[idx]
> ...
> }
>
> What is the preferred way to do this?
>
>
> _____________________________________________________________
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list