Re: [R] numbers loop in R

2009-04-17 Thread hadley wickham
On Fri, Apr 17, 2009 at 12:19 PM, jim holtman wrote: > try this: > >> matrixx<-function(A){ > +     B=matrix(NaN,nrow=(A+1),ncol=4) > +     k <- 1 > +     for (i in 3:A){ > +         for (j in i:A) { > +             B[k,] <- c(NaN, i-2, i-1, j) > +             k <- k + 1 > +         } > +     } >

Re: [R] numbers loop in R

2009-04-17 Thread jim holtman
try this: > matrixx<-function(A){ + B=matrix(NaN,nrow=(A+1),ncol=4) + k <- 1 + for (i in 3:A){ + for (j in i:A) { + B[k,] <- c(NaN, i-2, i-1, j) + k <- k + 1 + } + } + B + } > matrixx(5) [,1] [,2] [,3] [,4] [1,] NaN123 [

Re: [R] numbers loop in R

2009-04-17 Thread David Winsemius
I would have expected to see the assignment to B[k,] inside the loops. And to see some connection with the k index in the inner loops if you did not want all of the rows to be similar. Because the assignment is outside the loops, it happens only once. -- David Winsemius On Apr 17, 2009, a