laterstat laterstat - 5 months ago 42
R Question

Boolean operator OR on rows of matrices

I would like to use the Boolean R operator OR on rows of matrices. Below you find my solution to a simple toy example using the apply function. What is the easiest/fastest way of doing this? How does this generalize to other Boolean operations?

b <- matrix(c(T,T,F,T,F,F),3)

apply(b,1, function(x) T %in% x)


You can use the functions any (logical “or”) and all (logical “and”) for this. In your case:

apply(b, 1, any)

As an aside, I’d avoid using T and F in code: they are normal identifiers that can be redefined elsewhere in code. TRUE and FALSE on the other hand are not constants.