[R] eval and assign in loop problem

David Winsemius dwinsemius at comcast.net
Mon Jul 12 15:07:10 CEST 2010


On Jul 12, 2010, at 7:16 AM, S.Nicholas wrote:

> Thank you Erik and Patrick,
>
> using vectorization and apply function seem to be very promising, even
> though I don't know how to use apply/sapply for updating discrete time
> difference equation instead of using for loop.
> (e.g, Nt+1 = Nt (1+R(K-Nt/K)))
>
> Maybe I could use vector for different parameters,
> and for loop for updating the equation.

Loops methods:

  b0 <- rnorm(10)
  b1 <- c(0,b0[2:(length(b0)-1)] ) # shifted one time interval
  ?lag
  ?diff

See also section 4 (page 9) of this exposition for worked examples of  
using R for discrete-time difference equations:

http://www.soa.org/files/pdf/UsingRforActuarialScience.pdf

-- 
David

>
> Best,
>
> Nic
>
> On Sun, Jul 11, 2010 at 1:02 PM, Patrick Connolly <
> p_connolly at slingshot.co.nz> wrote:
>
>> 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
>>
>> ~ 
>> .~ 
>> .~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
>>
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list