[R] eval and assign in loop problem

Patrick Connolly p_connolly at slingshot.co.nz
Sun Jul 11 22:02:22 CEST 2010


On Fri, 09-Jul-2010 at 08:25AM -0700, S.Nicholas wrote:

|> deaR useRs,
|> 
|> I am trying to assign different values to different objects in a for loop.
|> The following is a toy example of the part that has been giving me a hard
|> time.
|> 
|> The first "for loop" generates four objects, b0, b1, b2, b3 with random
|> numbers.
|> And, the second "for loop" is equivalent to
|> b1 = b0
|> b2 = b1
|> b3 = b2
|> b4 = b3

I don't quite get the idea, but would you be able to simply reverse
the order of those last 4 (for i in 3:0)?  That way, they won't all be
overwritten with b0.

HTH



|> 
|> But, when I run this code, the result is equivalent to
|> b1 = b0
|> b2 = b0
|> b3 = b0
|> b4 = b0
|> 
|> So, the increment does not seem to be properly working for the second part
|> of the "assign" function.
|> Why would this be?
|> 
|> for (i in 0:3)
|>        {
|>            r = runif(1)
|>            assign(paste('b',i,sep=''),r)
|>        }
|> 
|> 
|> for (i in 1:4)
|>        {
|> 
|> assign(paste('b',i,sep=''),eval(parse(text=paste('b',i-1,sep=''))))
|>        }
|> 
|> Thank you.
|> 
|> Nic
|> 
|> 	[[alternative HTML version deleted]]
|> 
|> ______________________________________________
|> R-help at r-project.org mailing list
|> https://stat.ethz.ch/mailman/listinfo/r-help
|> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
|> and provide commented, minimal, self-contained, reproducible code.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list