An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110428/0f4fac1e/attachment.pl>
for loop with global variables
6 messages · Ivan, jim holtman, PIKAL Petr
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?
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110428/b1519e7a/attachment.pl>
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.
Hi r-help-bounces at r-project.org napsal dne 28.04.2011 16:59:26:
ivan <i.petzev at gmail.com> Odeslal: r-help-bounces at r-project.org 28.04.2011 16:59 Komu jim holtman <jholtman at gmail.com> Kopie r-help at r-project.org P?edm?t Re: [R] for loop with global variables Hi, thank you for the response. What I actually want to do is pick
automatically
the results of the various outputs (which I have already defined as variables, e.g. output.1,output.2,etc) and insert them into a table. The list variable would be redundant since in order to create the data frame
I
would rather type data.frame(results=rbind(output.1,output.2,etc.)). If
I
had 100 outputs, this would be much work. I rather meant something like
for(i in 1:100) {x=data.frame(results=rbind(output.[[i]]))}, which does
not
work though. It says that object "output." cannot be found. Kind Regards
That is the problem. If you used list in the first time you could save
yourself much problems. BTW - data frame is also a list with some special
formating.
To extend my example
for(i in 1:3) {
lll[[i]] <- sample(letters,3)
names(lll)[i] <- LETTERS[i]
}
lll
$A
[1] "x" "t" "h"
$B
[1] "c" "m" "w"
$C
[1] "n" "s" "e"
as.data.frame(lll)
A B C
1 x c n
2 t m s
3 h w e
Regards
Petr
On Thu, Apr 28, 2011 at 4:31 PM, jim holtman <jholtman at gmail.com> wrote:
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?
[[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.
Hi
Something like
paste("output", i, sep = ".")
shall work
You could consult FAQ
7.34 How can I save the result of each iteration in a loop into a separate
file
Regards
Petr
r-help-bounces at r-project.org napsal dne 28.04.2011 17:17:48:
Petr PIKAL <petr.pikal at precheza.cz> Odeslal: r-help-bounces at r-project.org 28.04.2011 17:17 Komu ivan <i.petzev at gmail.com> Kopie r-help at r-project.org P?edm?t Re: [R] for loop with global variables Hi r-help-bounces at r-project.org napsal dne 28.04.2011 16:59:26:
ivan <i.petzev at gmail.com> Odeslal: r-help-bounces at r-project.org 28.04.2011 16:59 Komu jim holtman <jholtman at gmail.com> Kopie r-help at r-project.org P?edm?t Re: [R] for loop with global variables Hi, thank you for the response. What I actually want to do is pick
automatically
the results of the various outputs (which I have already defined as variables, e.g. output.1,output.2,etc) and insert them into a table.
The
list variable would be redundant since in order to create the data
frame
I
would rather type data.frame(results=rbind(output.1,output.2,etc.)).
If
I
had 100 outputs, this would be much work. I rather meant something
like
for(i in 1:100) {x=data.frame(results=rbind(output.[[i]]))}, which
does
not
work though. It says that object "output." cannot be found. Kind Regards
That is the problem. If you used list in the first time you could save yourself much problems. BTW - data frame is also a list with some
special
formating.
To extend my example
for(i in 1:3) {
lll[[i]] <- sample(letters,3)
names(lll)[i] <- LETTERS[i]
}
lll
$A
[1] "x" "t" "h"
$B
[1] "c" "m" "w"
$C
[1] "n" "s" "e"
as.data.frame(lll)
A B C
1 x c n
2 t m s
3 h w e
Regards
Petr
On Thu, Apr 28, 2011 at 4:31 PM, jim holtman <jholtman at gmail.com>
wrote:
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?
[[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.