Skip to content
Back to formatted view

Raw Message

Message-ID: <42824226.2060303@lancaster.ac.uk>
Date: 2005-05-11T17:34:30Z
From: Barry Rowlingson
Subject: simplify loop
In-Reply-To: <3f87cc6d05051110054d65813a@mail.gmail.com>

Omar Lakkis wrote:
> Is there a way to implement this faster than doing it in a loop. 
> 
>         for (i in length(settle)-1:1) {
>                 settle[i] = settle[i+1]/(1 + settle.pct[i+1])
>         }

  You dont need a loop at all here. How so? Well, as it is written the 
code in the for loop only executes once:

  > settle=1:10
  > for (i in length(settle)-1:1) {
  + print(i)
  + }
  [1] 9
  >

  you have made a mistake, and I think you really want:

  > for (i in (length(settle)-1):1 ) {

  since R does the ':' first and then does 'length(settle)-' unless you 
bracket it:

   > length(settle)-1:1
   [1] 9
   > (length(settle)-1):1
   [1] 9 8 7 6 5 4 3 2 1

I don't see an obvious way to get rid of the for loop though...

Baz