[R] Assignment to a list in a function

Heywood, Giles giles.heywood at CommerzbankIB.com
Wed Nov 28 18:16:07 CET 2001

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

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

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

More information about the R-help mailing list