[R] How to use ifelse without invoking warnings
Sarah Goslee
@@r@h@go@|ee @end|ng |rom gm@||@com
Thu Oct 7 19:38:34 CEST 2021
Bert's approach is much less risky!
On Thu, Oct 7, 2021 at 1:37 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:
>
> ?suppressWarnings
>
> > p <- .05
> > 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
> >
> > suppressWarnings(ans <- ifelse (k >= -1 & k <= n,
> 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 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.
> >
>
> [[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