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

[R] numbers loop in R

2009-04-17 Thread emj83
I would like to create a matrix in R that looks similar to this: [,1] [,2] [,3] [,4] [1,] NaN 1 2 3 [2,] NaN 1 2 4 [3,] NaN 1 2 5 [4,] NaN 2 3 4 [5,] NaN 2 3 5 [6,] NaN345 I have the loop below: where A for example is 5 matrixx<-function(A){ B=matrix(NaN,