Ross Bennett — written Dec 23, 2012 — source
The sugar function any() answers the question, “Are any of the values … ?”.
The any_sug() function takes a LogicalVector as an argument and allows one to enter an expression for the argument as shown in the R examples. In this example, it is simply wrapper around the sugar function any() and includes is_true to return a boolean.
Note that when comparing two vectors, it is an element-wise comparison.
(i.e. x[0] > y[0]
, …, x[n] > y[n]
)
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
While the above function may seem trivial, it can be easy to forget is_true() when using any() and will result in a compile error. The check_negative() function below is an example of a simple utility function to check if a vector contains negative values using the any_sug() function defined above.
The vector contains negative numbers
The vector does not contain negative numbers
tags: sugar
Tweet