Skip to content

How to use ifelse without invoking warnings

4 messages · Ravi Varadhan, Bert Gunter, Sarah Goslee

#
Hi,
I would like to execute the following vectorized calculation:

  ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), ifelse (k < -1, 0, 1) )

For example:
Warning message:
In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
[1] 0.000000000 0.006821826 0.254991551 1.000000000

The answer is correct.  However, I would like to eliminate the annoying warnings.  Is there a better way to do this?

Thank you,
Ravi
#
?suppressWarnings
ifelse (k < -1, 0, 1) )
Warning message:
In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) ))
## no warnings

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Oct 7, 2021 at 10:21 AM Ravi Varadhan via R-help <
r-help at r-project.org> wrote:

            

  
  
#
If you are positive the warnings don't matter for your application,
you can disable them: see ?options for details of warn.

But that can be dangerous, so be careful!

Sarah

On Thu, Oct 7, 2021 at 1:21 PM Ravi Varadhan via R-help
<r-help at r-project.org> wrote:

  
    
#
Bert's approach is much less risky!
On Thu, Oct 7, 2021 at 1:37 PM Bert Gunter <bgunter.4567 at gmail.com> wrote: