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)
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?
Thank you,
ThomasThis message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.
For loop indicies
5 messages · Andris Jankevics, Sarah Goslee, Enrico Schumann +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20111212/4505372f/attachment.pl>
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
You may want to check how your loop "misses out" the zero: C <- 5 for (i in 0:C) print(i) ## gives me [1] 0 [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 Regards, Enrico Am 12.12.2011 12:44, schrieb Thomas Chesney:
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)
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?
Thank you,
ThomasThis message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
This message has been checked for viruses but the contents of an attachment
may still contain software viruses which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.
______________________________________________ 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.
Enrico Schumann Lucerne, Switzerland http://nmof.net/
Ah. I did a Silly Thing, and thank you for all the responses helping me track it down. The issue lay not in the for (i in 1:C) but in the line VectorName[i] <- ... That's the one that didn't like 0. Thomas
From: Enrico Schumann [enricoschumann at yahoo.de]
Sent: Monday, December 12, 2011 11:59 AM
To: Thomas Chesney
Cc: r-help at r-project.org
Subject: Re: [R] For loop indicies
Sent: Monday, December 12, 2011 11:59 AM
To: Thomas Chesney
Cc: r-help at r-project.org
Subject: Re: [R] For loop indicies
You may want to check how your loop "misses out" the zero:
C <- 5
for (i in 0:C) print(i)
## gives me
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Regards,
Enrico
Am 12.12.2011 12:44, schrieb Thomas Chesney:
> 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)
>
> 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?
>
> Thank you,
>
> ThomasThis message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.
> ______________________________________________
> 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.
>
--
Enrico Schumann
Lucerne, Switzerland
http://nmof.net/