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?
Thanks.
[[alternative HTML version deleted]]
Yes you can, but there is not enough explaination as to what you
really want to do. I would suggest that you look at using a 'list'
instead of individual objects:
myList <- list(output.1 = rbind('a','b'), output.2 =
rbind('c','d'), ...)
Then you can use 'lapply' to operation on the elements.
On Thu, Apr 28, 2011 at 10:16 AM, ivan <i.petzev at gmail.com>
wrote:> 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?
>
> 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.
>
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.