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:
Hi, I try to make a vector in a for for loop, but it dont work. Look:
a <- 0;for(i in c(1:2)) { for(j in c(1:2)) { a <- i+j; print(a)}}
[1] 2 [1] 3 [1] 3 [1] 4 I try to make this a vector, like this: [1] 2 3 3 4
a <- 0;for(i in c(1:2)) { for(j in c(1:2)) { a[j] <- i+j}}; print(a)
[1] 3 4
a <- 0;for(i in c(1:2)) { for(j in c(1:2)) { a[i] <- i+j}}; print(a)
[1] 3 4 In this way the vector have only the two last loop. I try another way but it dont work. How make a correct index for this loop inside loop? Thanks Ronaldo -- As coisas por aqui n?o andam, arrastam-se lentamente em dire??o a lugar nenhum. Eu hoje acordei assim, otimista... -- | // | \\ [*****************************][*******************] || ( ? ? ) [Ronaldo Reis J?nior ][PentiumIII-600 ] | V [UFV/DBA-Entomologia ][HD: 30 + 10 Gb ] || / \ [36571-000 Vi?osa - MG ][RAM: 128 Mb ] | /(.''`.)\ [Fone: 31-3899-2532 ][Video: SiS620-8Mb ] ||/(: :' :)\ [chrysopa at insecta.ufv.br ][Modem: Pctel-onboar] |/ (`. `'` ) \[ICQ#: 5692561 ][Kernel: 2.4.18 ] || ( `- ) [*****************************][*******************] ||| _/ \_Powered by GNU/Debian W/Sarge D+ || Lxuser#: 205366
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help