[R] Odp: for loop with global variables

Petr PIKAL petr.pikal at precheza.cz
Thu Apr 28 17:06:35 CEST 2011


Hi

r-help-bounces at r-project.org napsal dne 28.04.2011 16:16:16:

> ivan <i.petzev at gmail.com> 
> Odeslal: r-help-bounces at r-project.org
> 
> 28.04.2011 16:16
> 
> Komu
> 
> r-help at r-project.org
> 
> Kopie
> 
> Předmět
> 
> [R] for loop with global variables
> 
> Hi,
> 
> is there a possibility to use global variables in a for loop. More
> specifically, I want to do the following:
> 
> output.1<-rbind("a","b")
> output.2<-rbind("c","d")
> output.3<-rbind("e","f")
> .
> .
> .
> output.n<-rbind(...,...)
> 
> next I want to create a data frame with two columns:
> 
> Outputs
> Values  output.1 "a","b"  output.2 "c","d"  output.3 "e","f"  .
>  .
>  output.n …,…
> My problem is that I do not how to define a loop over global variables.
> Anybody an idea?

Don't do that. As Jim suggested use list inside a loop.

#declare a list
lll<-vector("list",3)

#do your loop
for(i in 1:3) lll[[i]] <- letters[i]

lll
[[1]]
[1] "a"

[[2]]
[1] "b"

[[3]]
[1] "c"

But I feel that you could maybe achieve desired result without loop in 
more Rish way. If what you want to do is reasonable and frequent I believe 
somebody already made a function which does what you want.

Regards
Petr


> 
> Thanks.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list