# [Rd] function(x) !is.na(x) & x "is_true(.)" etc. was: [R] How to index..

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Fri Feb 21 12:11:24 CET 2020

```Diverted from R-help to R-devel,
as I've changed the focus to consider adding new functions to R :

>>>>> peter dalgaard
>>>>>     on Fri, 21 Feb 2020 10:46:16 +0100 writes:

> It has isTRUE, but that is not vectorized, and in fact explicitly tests length==1, so
>> isTRUE(c(TRUE,FALSE,NA))
> [1] FALSE
>> isTRUE(c(TRUE,TRUE, TRUE)) # I thought I thaw a puddycat... ;-)
> [1] FALSE
>> Vectorize(isTRUE)(c(TRUE,FALSE,NA))
> [1]  TRUE FALSE FALSE

> (The latter would be silly as an implementation of is_true, of course.)

> -pd

yes... We've had (hidden) functions in the Matrix package for
these, which can still be accelerated (quite a bit for most
arguments), in Matrix/R/Auxiliaries.R (see the R-forge development version :
https://r-forge.r-project.org/scm/viewvc.php/pkg/Matrix/R/Auxiliaries.R?view=markup&root=matrix  )

## Need to consider NAs ;  "== 0" even works for logical & complex:
## Note that "!x" is faster than "x == 0", but does not (yet!) work for complex
## if we did these in C, would gain a factor 2 (or so):
is0  <- function(x) !is.na(x) & x == 0
isN0 <- function(x)  is.na(x) | x != 0
is1  <- function(x) !is.na(x) & x   # also == "isTRUE componentwise"

{{Note that here  0 <==> FALSE   and  non-0 <==> TRUE , and I
had preferred the shorter words to the longer ones in the
Matrix pkg, not the least as '0' is relevant also for
}}

... and then there are  all* and any* versions of these
functions there , for which I have even written fast C based .Call()s ...

May this be something worth adding to base  for  R 4.0.0
(if only just to entice more users making the effort to switch
to it in ca. 2 months) ?

Martin

>> On 20 Feb 2020, at 04:20 , Lijun Zhao <lijun.zhao using adelaide.edu.au> wrote:
>>
>>
>> Some packages have the equivalent of that is_true function, which maps FALSE and NA to FALSE and TRUE to TRUE.  I don't think core R contains such a function.

> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

```