[R] Assignment to a list in a function

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Nov 28 18:46:49 CET 2001


"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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list