Combine logical vectors in list using logical OR
How about Reduce:
Reduce("&", opts)
Reduce("|", opts)
If all the lists are the same length, you can cast it to a data frame, and then use any
:
apply(data.frame(opts),1,any)
Edit: while I thought this might be fast because it avoids cbind
, it turns out that this is the slowest by far of the three solutions, according to my benchmarking:
set.seed(123)
opts = as.list(as.data.frame(matrix(sample(c(TRUE, FALSE), 10000, replace=TRUE), nrow=1000)))
require(microbenchmark)
microbenchmark(Reduce("|",opts),rowSums(do.call(cbind, opts)) > 0,
apply(as.data.frame(opts),1,any))
Unit: microseconds
expr min lq median uq
Reduce("|", opts) 99.200 101.0780 106.596 110.3725
rowSums(do.call(cbind, opts)) > 0 209.326 211.9665 217.329 224.0505
apply(as.data.frame(opts), 1, any) 4130.429 4245.7380 4308.054 4438.2485
max neval
120.63 100
237.19 100
6949.19 100