On Sat, Oct 9, 2021 at 3:00 AM Ravi Varadhan via R-help <r-help@r-project.org> wrote: > > Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but > I felt that > there must be a more principled approach. While John's solution is what I > would prefer, > I cannot help but wonder why `ifelse' was not constructed to avoid this > behavior.
Note that John's approach is mentioned twice in the help page for ifelse, once in the examples, and once quite early on in the "Warning" section: Sometimes it is better to use a construction such as (tmp <- yes; tmp[!test] <- no[!test]; tmp) , possibly extended to handle missing values in 'test'. Best, -Deepayan > Thanks & Best regards, > Ravi > ________________________________ > From: John Fox <j...@mcmaster.ca> > Sent: Thursday, October 7, 2021 2:00 PM > To: Ravi Varadhan <ravi.varad...@jhu.edu> > Cc: R-Help <r-help@r-project.org> > Subject: Re: [R] How to use ifelse without invoking warnings > > > External Email - Use Caution > > > > Dear Ravi, > > It's already been suggested that you could disable warnings, but that's > risky in case there's a warning that you didn't anticipate. Here's a > different approach: > > > kk <- k[k >= -1 & k <= n] > > ans <- numeric(length(k)) > > ans[k > n] <- 1 > > ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE) > > ans > [1] 0.000000000 0.006821826 0.254991551 1.000000000 > > BTW, I don't think that you mentioned that p = 0.3, but that seems > apparent from the output you showed. > > I hope this helps, > John > > -- > John Fox, Professor Emeritus > McMaster University > Hamilton, Ontario, Canada > web: > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsocialsciences.mcmaster.ca%2Fjfox%2F&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160038474%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=Q33yXm36BwEVKUWO72CWFpSUx7gcEEXhM3qFi7n78ZM%3D&reserved=0 > > On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160048428%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=FXX%2B4zNT0JHBnDFO5dXBDQ484oQF1EK5%2Fa0dG9P%2F4k4%3D&reserved=0 > > PLEASE do read the posting guide > > https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160048428%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=ss2ohzJIY6qj0eAexk4yVzTzbjXxK5VZNors0GpsbA0%3D&reserved=0 > > and provide commented, minimal, self-contained, reproducible code. > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.