[R] How to use ifelse without invoking warnings

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Thu Oct 7 19:34:35 CEST 2021


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 using r-project.org> wrote:
>
> 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:
>
>
> > k <- c(-1.2,-0.5, 1.5, 10.4)
> > n <- 10
> > ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse (k < -1, 0, 1) )
> Warning message:
> In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> > print(ans)
> [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
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



-- 
Sarah Goslee (she/her)
http://www.sarahgoslee.com



More information about the R-help mailing list