Thanks to Rich, Douglas and Erin. Off course the problem was the index! I
was looking at the wrong place!! Thanks for your help!
Francisco
From: Rich FitzJohn <[EMAIL PROTECTED]>
Reply-To: Rich FitzJohn <[EMAIL PROTECTED]>
To: "Francisco J. Zagmutt" <[EMAIL PROTECTED]>
CC: R-help@stat.math.ethz.ch
Subject: Re: [R] i param in "for" loop does not takes zeros?
Date: Thu, 14 Apr 2005 09:36:21 +1200
The for loop is not ignoring the zero at all, but the assignment is,
since R indexes starting at 1, not zero.
> sim <- c()
> sim[0] <- 1
> sim
numeric(0)
To run this loop this way, you need to add one to the index:
for ( i in 0:5 )
sim[i+1] <- dbinom(i, 5, p)
However, you'd be better off passing your vector of values directly to
dbinom():
> dbinom(0:5, 5, p)
[1] 0.32768 0.40960 0.20480 0.05120 0.00640 0.00032
> all(dbinom(0:5, 5, p) == sim)
[1] TRUE
Cheers,
Rich
On 4/14/05, Francisco J. Zagmutt <[EMAIL PROTECTED]> wrote:
> Hi all
>
> Is there any reason why the parameter i in a "for" loop ignores a value
of
> zero? For example
>
> sim=c()
> p=.2
> for(i in 0:5)
> {sim[i]=dbinom(i,5,p)
> }
>
> sim
> [1] 0.40960 0.20480 0.05120 0.00640 0.00032
>
> In this example the quantile i= 0 was ignored since
> dbinom(0,5,p)
> [1] 0.32768
>
> The same behaviour occurs if I use a while loop to perform the same
> calculation:
> sim=c()
> p=.2
> i=0
> while(i <6)
> {sim[i]=dbinom(i,5,p)
> i=i+1
> }
> sim
> [1] 0.40960 0.20480 0.05120 0.00640 0.00032
>
> How can I perform a loop passing a zero value parameter? I know I can
use
> an if statement for i<=0 but I was wondering why the loop is ignoring
the
> zero value.
>
> Many thanks!
>
> Francisco
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
--
Rich FitzJohn
rich.fitzjohn <at> gmail.com |
http://homepages.paradise.net.nz/richa183
You are in a maze of twisty little functions, all
alike
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html