From rgentlem@hsph.harvard.edu  Thu Apr 22 16:14:51 1999
From: rgentlem@hsph.harvard.edu (Robert Gentleman)
Date: Thu, 22 Apr 1999 11:14:51 -0400 (EDT)
Subject: Assigning to a list within a loop (PR#175)
In-Reply-To: <199904220345.FAA28639@pubhealth.ku.dk>
Message-ID: <Pine.SV4.4.05.9904221109250.7304-100000@hsph.harvard.edu>



On Thu, 22 Apr 1999 wvenable@arcola.stats.adelaide.edu.au wrote:

> We noticed this bug in more complex code, but this succinctly
> describes the phenomenon.
> 
> If you do not re-initialize a each time in the following loop,
> the final value only is assigned to all places when the loop is
> complete.
> 
> > a <- b <- list()
> > for(i in 1:5) {
> +	a$alpha <- i
> +	b[[i]] <- a
> +	}
> > unlist(b)
> alpha alpha alpha alpha alpha 
>     5     5     5     5     5 
> 
> If a is re-created each time, however the correct behaviour
> happens:
> 
> 
> > b <- list()
> > for(i in 1:5) {
> +	  a <- list(alpha = i)
> +	  b[[i]] <- a
> +	  }
> > unlist(b)
> alpha alpha alpha alpha alpha 
>     1     2     3     4     5 
> 
> > version
>            _                 
> platform   i586-unknown-linux
> arch       i586              
> os         linux             
> system     i586, linux       
> status                       
> status.rev 0                 
> major      0                 
> minor      64.0              
> year       1999              
> month      April             
> day        8                 
> language   R                 
> >
> 
> Bill Venables & Berwin Turlach.
> 
  I think that worse things could happen, the problem seems to be around
 line 1486 of subassign (the first line is missing in the current source)

  1486              if( NAMED(y) ) y = duplicate(y);
  1487              VECTOR(x)[offset] = y;
  1488              break;
  1489

  I think that you need to duplicate y, and have added that line;
  but perhaps what you need to do is to increment NAMED of y(???)
  I'll commit it if Ross doesn't yell too loud

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


Assigning to a list within a loop (PR#175)
Robert Gentleman