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.
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.
Physical address: Postal address:
CSIRO Marine Laboratories, PO Box 120,
233 Middle St, Cleveland, Queensland Cleveland, Qld, 4163
AUSTRALIA AUSTRALIA
Telephone: +61 7 3826 7251 Email: Bill.Venables@cmis.csiro.au
Fax: +61 7 3826 7304
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._