[R] How to use ifelse without invoking warnings

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Oct 9 00:45:44 CEST 2021


I don't think it is avoidable... ifelse cannot figure out which elements will exist in the second and third arguments until they are evaluated, so it cannot make use of the TRUE/FALSE information until both possible output vectors have been evaluated, which is a performance hit and yields warnings or errors.

You, on the other hand, can figure that out and use input validation to limit which inputs get fed into the warning- or error- producing algorithm and put the results into a final result vector using indexed assignment as Dr Fox suggested.


On October 8, 2021 5:21:44 AM PDT, Ravi Varadhan via R-help <r-help using 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.
>
>Thanks & Best regards,
>Ravi
>________________________________
>From: John Fox <jfox using mcmaster.ca>
>Sent: Thursday, October 7, 2021 2:00 PM
>To: Ravi Varadhan <ravi.varadhan using jhu.edu>
>Cc: R-Help <r-help using 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 using 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 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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list