Assignment to a list in a function does not behave as I would expect. The following function illustrates the point #---------------------------------------- temp _ function() { nf _ 2 ns _ 5 z _ vector("list",ns) x _ matrix(NA,nrow=ns,ncol=nf) for(j in 1:ns) { for(i in 1:nf) { x[,i] _ i+j*100 } z[[j]] _ x print(z[[j]]) } z } #------------------------------------------ Replacing the line that assigns z[[j]] with the following 2 lines produces the expected result y _ x z[[j]] _ y Furthermore, I get the expected result by running the function under Splus 6. My question is: do we have an instance of assignment by reference here? Is this behaviour intended? Giles Heywood ********************************************************************** This communication is confidential and is intended only for the person to whom it is addressed. If you are not that person you are not permitted to make use of the information and you are requested to notify <mailto:LONIB.Postmaster at commerzbankib.com> immediately that you have received it and then destroy the copy in your possession. Commerzbank AG is regulated by the SFA for the conduct of investment business in the UK. ********************************************************************** -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Heywood, Giles" <giles.heywood at CommerzbankIB.com> writes:> Assignment to a list in a function does not behave as I would expect. > > The following function illustrates the point > > #---------------------------------------- > temp _ function() > { > nf _ 2 > ns _ 5 > z _ vector("list",ns) > x _ matrix(NA,nrow=ns,ncol=nf) > for(j in 1:ns) > { > for(i in 1:nf) > { > x[,i] _ i+j*100 > } > > z[[j]] _ x > > print(z[[j]]) > } > z > } > #------------------------------------------ > > Replacing the line that assigns z[[j]] with the following 2 lines produces > the expected result > > y _ x > z[[j]] _ y > > Furthermore, I get the expected result by running the function under Splus > 6.Um, you're not telling us what you're expecting, nor what you're getting, nor what is supposedly wrong, nor which version of R this relates to, nor the operating system.... Running your code (after removing the tab and underscore damage) I get exactly the same result with or without the intermediate assignment. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help 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-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._