On 20/04/2010 4:27 AM, n.vialma at libero.it wrote:> Hi all,
> I have a question about global and local variables.First of all, a variable
> defined in a for loop is it a local or global variable??
>
R doesn't create a local scope for "for" loops. So it will have
the
same scope as other variables created in the same scope.> Second, I'm trying to build a loop in the following way:
> I have these 3 data frames
> bilanci_2005<-bilanci1[ANNO==2005,]
> bilanci_2006<-bilanci1[ANNO==2006,]
> bilanci_2007 <--bilanci1[ANNO==2007,]
>
> LOOP:
> v=list(2005,2006,2007)
>
I would recommend using c(2005,2006, 2007) rather than list(), but I
don't think that has much effect.> for (a in v){
> anno1=a
> anno2=a+1
> for (i in 1:length(bilanci_a)) {
>
Using the name of one variable as part of another name won't cause it to
be magically expanded. You should have set things up differently from
the beginning:
bilanci_subsets <- list("2005" = bilanci1[ANNO==2005,],
"2006" = bilanci1[ANNO==2006,],
"2007" = bilanci1[ANNO==2007,] )
Then you can use
for (i in names(bilanci_subsets)) {
.... bilanci_subsets[[i]] ....
Duncan Murdoch> assign(paste(""bilanci",anno1,anno2,sep=""),
rbind(paste("bilanci",anno1,anno2,
> sep=""),
>
bilanci_anno2[bilanci_anno2$CFISCALE==bilanci_anno1
> [i,]$CFISCALE,],
> "bilanci_anno1[i,])
> }
> }
> what I'm trying to do is for example to give the contents of a (so
the
> values which are in the list named v) to the bilanci variable. In Stata
this
> means using a particular kind of "".
> How can I do this in R?
>
> ______________________________________________
> 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.
>