[R] Odp: for loop with global variables

From: Petr PIKAL <petr.pikal_at_precheza.cz>
Date: Thu, 28 Apr 2011 17:06:35 +0200

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.



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. Received on Thu 28 Apr 2011 - 15:09:16 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Apr 2011 - 15:20:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive