[R] How to use ifelse without invoking warnings

Ravi Varadhan r@v|@v@r@dh@n @end|ng |rom jhu@edu
Thu Oct 7 18:29:04 CEST 2021


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]]



More information about the R-help mailing list