Skip to content
Back to formatted view

Raw Message

Message-ID: <CAM_vjumwX42O1qPVZ3kWST=P0X3WaSBNeWF0CSzfTMno2FkTTw@mail.gmail.com>
Date: 2011-12-12T11:53:18Z
From: Sarah Goslee
Subject: For loop indicies
In-Reply-To: <5EAA21940C65214F9C11DA5FBBC14F0B3004573DFB@EXCHANGE2.ad.nottingham.ac.uk>

Hi,

On Mon, Dec 12, 2011 at 6:44 AM, Thomas Chesney
<Thomas.Chesney at nottingham.ac.uk> wrote:
> I would like to run a for loop with an index going from 0 to 499 but the following seems to miss out the first value:
>
> C <- 499
> for (i in 0:C)

First off, you've named your variable for an existing function, which
can cause all kinds of problems.

Second, this should work just fine, as it does for me:

> maxvar <- 4
> for(i in 0:maxvar) cat(i, "\n")
0
1
2
3
4

What leads you to think that it's not working?

> The alternative is:
>
> C <- 500
> for (i in 1:C)
> {
> #Then every time I use i, I replace it with i-1
> }
>
> Is this a good way to do it or is tere a better way?

There's a better way to ask your question, at least: give us
reproducible code, and explain what you are not seeing that you expect
to see, or vice versa. "seems to miss out on the first value" - what
happens? If I can't reproduce it, I can't help you solve it.

Sarah
-- 
Sarah Goslee
http://www.functionaldiversity.org