In R, I have a number of logical vectors, or varying number (ie sometimes 1, sometimes n vectors), they are guaranteed to be ALL of the same length, I need to produce a single vector of the same length as the input vectors, where each element is to be TRUE if any of the vectors at the same element index are TRUE, else FALSE.
I am wondering if there is a builtin operation, or simpler method, to achieve what I want. Below is what I have so far, for 3 vectors.
set.seed(1) #For reproducability
o = c(T,F)
l = 10
A = sample(o,l,replace=T)
B = sample(o,l,replace=T)
C = sample(o,l,replace=T)
fun = function(...) apply(do.call(cbind,args = list(...)),1,any)
fun(A,B,C) ##Produces Desired Result
We can use Reduce with |
Reduce(`|`, list(A, B, C))
Or with rowSums
rowSums(cbind(A,B,C))!=0
If there are only 3 vectors, a compact option would be
!!(A+B+C)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With