[R] how to skip part of the code

David Winsemius dwinsemius at comcast.net
Thu Mar 21 17:13:31 CET 2013


On Mar 21, 2013, at 8:30 AM, Andras Farkas wrote:

> Pikal and Robert,
>  
> sorry for previous email, indeed, the code is working just fine!
>  
> Thanks for the pointer David Winsemius, you were right, I was getting a character as "numeric(0)", which the system did not recognize as the number 0, thus the openBUGS run was called. 

That is NOT zero. Actually you were getting a vector of length == 0. 

> numeric(0) == 0
logical(0)
> isTRUE(numeric(0) == 0)
[1] FALSE

Some sort of selection activity apparently found no matches:

>  (1:10)[(1:10) < 0]
integer(0)

Another potential source of error would be passing an alphabetic value:

 "e" > 0
#[1] TRUE

-- 
David.

>  
> Thanks for the help guys!
>  
> Andras
> 
> --- 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
Alameda, CA, USA



More information about the R-help mailing list