Skip to content
Prev 31603 / 398513 Next

Help to make a for for index

Printing some intermediate expressions can make it clearer what R is trying 
to do:

 > a <- 0;for(i in c(1:2)) { for(j in c(1:2)) { e<-substitute(a[j] <- 
i+j,list(i=i,j=j)); cat("executing:", deparse(e),"\n"); eval(e)}}; print(a)
executing: a[1] <- 1 + 1
executing: a[2] <- 1 + 2
executing: a[1] <- 2 + 1
executing: a[2] <- 2 + 2
[1] 3 4
 >

Perhaps this is what you want: (?)

 > a <- numeric(0);for(i in c(1:2)) { for(j in c(1:2)) { a <- c(a,i+j); 
print(a)}}
[1] 2
[1] 2 3
[1] 2 3 3
[1] 2 3 3 4
 > a
[1] 2 3 3 4
 >

(This is not the most efficient way to construct a vector in a loop, but it 
works and the code captures the spirit of what I'm guessing you were trying 
to do.)

hope this helps,

-- Tony Plate
At Monday 07:22 PM 5/5/2003 -0300, Ronaldo Reis Jr. wrote: