[R] how to skip part of the code

David Winsemius dwinsemius at comcast.net
Wed Mar 20 16:15:17 CET 2013


On Mar 20, 2013, at 9:52 AM, Andras Farkas wrote:

> Dear Pikal and Robert,
>
> thanks for the help, I guess trying the if function is worth it, and  
> indeed, it works for the example I provided. Unfortunatelly though I  
> am not getting the expected results with my actual code, which is a  
> lot more complicated using R2OpenBUGS. Let me give you the actual  
> example:
>
> using my model and input parameters I am generating res with a bugs  
> function:
>
> res <-bugs(list(....)
>
> and also have:
>
> k <-5+2
>
> I have tryed your code as follows:
>
> if (e > 0) { res <-bugs(list(....); k <-5+2 }
>
> my goal is that if e = 0, then not to prompt a call of the model in  
> OpenBUGS, which is what is happening currently with the way it is  
> written here. Any thoughts on how to completiley "ignorre" a that  
> line or chunk?

That code _should_ have the desired avoidance of a call to bugs(). Are  
you perhaps dealing with a samll non-zero 'e' that prints as zero at  
the console? Check Faq 7.31.

Also:

?all.equal
?zapsmall


-- 
David.
> --- On Wed, 3/20/13, Robert Baer <rbaer at atsu.edu> wrote:
>
>
> From: Robert Baer <rbaer at atsu.edu>
> Subject: Re: [R] how to skip part of the code
> To: "PIKAL Petr" <petr.pikal at precheza.cz>
> Cc: "Andras Farkas" <motyocska at yahoo.com>, "r-help at r-project.org" <r-help at r-project.org 
> >
> Date: Wednesday, March 20, 2013, 9:27 AM
>
>
> On 3/20/2013 8:21 AM, PIKAL Petr wrote:
>> Hi
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>> project.org] On Behalf Of Andras Farkas
>>> Sent: Wednesday, March 20, 2013 2:11 PM
>>> To: r-help at r-project.org
>>> Subject: [R] how to skip part of the code
>>>
>>> Dear All,
>>>
>>> another quick question, this one is on skipping part of my code,  
>>> so let
>>> us say:
>>>
>>> a <-5
>>> b <-2
>>> e <-0
>>>
>>> d <-a+b
>>> f <-a-b
>>>
>>> what I would like to do is to have R NOT to calculate the value  
>>> for d
>>> in case the value of e equals to zero (essentially skip that  
>>> "chunk"),
>>> but instead move on to calculate te value for f. In the code I am
>>> working with the value of e changes, and I would like to calculate d
>>> and f at all times when the value of e is greater then zero. If
>>> possible, I would like to do this without using the functions  
>>> "ifelse"
>>> and "if else"
>> Why? This is exactly the reason for which if else was invented?
>>
>> I am not sure if some simple solution without if is available.
>>
>> if (e > 0) { d <- a+b; f <- a-b }
>>
>> seems to be simple.
>>
>> Regards
>> Petr
> I second Petr on the question, why not use if?  But this might meet  
> your
> criteria.
> a <- 5
> b <- 2
> e <- 0
>
> #
> dat <- data.frame(a, b, e)
>
>
> dat$d[dat$e  > 0] <- a + b
> dat$f <- a - b
>
>
>
>>> appreciate the help,
>>>
>>> Andras
>>>     [[alternative HTML version deleted]]
>> ______________________________________________
>> 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.
>
>
> -- 
>
> Robert W. Baer, Ph.D.
> Professor of Physiology
> Kirksille College of Osteopathic Medicine
> A. T. Still University of Health Sciences
> Kirksville, MO 63501 USA
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list