[R] Help to make a for for index
Spencer Graves
spencer.graves at pdf.com
Tue May 6 00:46:31 CEST 2003
Two alternative solutions:
as.vector(outer(1:2, 1:2, "+"))
a <- rep(NA, 4)
i1 <- 0
for(i in 1:2)for(j in 1:2){
i1 <- i1+1
a[i1] <- i+j
}
a
hth. spencer graves
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
More information about the R-help
mailing list