John - 7 months ago 26

R Question

If I have a vector of logical statements, like

`x>1`

`y!=1`

`sin(x)<0`

`AND(x>1, y!=1, sin(x)<0)`

with the same results as

`x>1 & y!=1 & sin(x)<0`

Answer

Yes, we can use `Reduce`

and `"&"`

:

```
AND <- function (...) Reduce("&", list(...))
```

Example:

```
AND(1 < 4, 5 > 7, 6 == 6)
# [1] FALSE
```

The **binary** operator `"&"`

only works with two inputs, so we can do

```
"&"(1 < 4, 5 > 7)
```

but not

```
"&"(1 < 4, 5 > 7, 6 == 6)
```

`Reduce`

is here to help, by iteratively applying `"&"`

:

```
tmp <- "&"(1 < 4, 5 > 7)
"&"(tmp, 6 == 6)
```