Message-ID: <OF0469907D.11E5966B-ONC1257880.00518BED-C1257880.0053198C@precheza.cz>
Date: 2011-04-28T15:06:35Z
From: PIKAL Petr
Subject: Odp: for loop with global variables
In-Reply-To: <BANLkTimHtvA5Dv4C+eV4jrza7_M0d6d+Mw@mail.gmail.com>
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.