s-news
[Top] [All Lists]

Re: Simple conditional loop

To: "Huso, Manuela" <manuela.huso@oregonstate.edu>
Subject: Re: Simple conditional loop
From: Patrick Burns <pburns@pburns.seanet.com>
Date: Tue, 29 May 2007 20:46:34 +0100
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <4D5DA98A54374044B7CC3F40A157B98BE77C6A@thuja>
References: <8C96912ADB6C413-9DC-7C31@mblk-r19.sysops.aol.com> <4D5DA98A54374044B7CC3F40A157B98BE77C6A@thuja>
User-agent: Mozilla Thunderbird 1.0.7 (Windows/20050923)
I think the question is more, "Why does it work in R?"


if(fallen[i-1] == 1) { .... } else { .... }

is what you are really trying to say.

But it looks to me like you can tell how many ones there
should be with:

log(runif(1)) / .94


Patrick Burns
patrick@burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Huso, Manuela wrote:

Dear S-news,
I want to create a vector of 0's and 1's whose elements are conditional on the previous element. I start with a vector with 10 elements, the first of which is a 1. After that I want the following elements to be 1 with probability 0.94. If any element becomes a 0, then all succeeding elements should be 0. I wrote the following which gave me fits trying to figure out why it doesn't work. I don't get any error messages, it just doesn't change my 'fallen' vector. fallen<-c(1,rep(0,9)) for (i in 2:10) ifelse(fallen[i-1]==1,fallen[i]<-runif(1)<0.94,fallen[i]<-0) Then I tried it in R. It worked perfectly. Can anyone tell me why it doesn't work in S+, please? Many thanks, Manuela

*>::<>::<>::<>::<>::<>::<>::<>::<>::<>::<*
*Manuela Huso*
*Consulting Statistician*
*Department of Forest Science*
*201H Richardson Hall*
*Oregon State University*
*Corvallis, OR   97331*
*ph: 541.737.6232*
*fx: 541.737.1393*



<Prev in Thread] Current Thread [Next in Thread>