Skip to content

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

r-help-bounces at r-project.org napsal dne 28.04.2011 16:16:16:
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
http://www.R-project.org/posting-guide.html
#
Hi

r-help-bounces at r-project.org napsal dne 28.04.2011 16:59:26:
automatically
I
I
not
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
...)
variables.
http://www.R-project.org/posting-guide.html
#
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:
The
frame
If
like
does
special
wrote:
http://www.R-project.org/posting-guide.html