Combine a large list of boolean vectors
I have a large list of boolean vectors TRUE/FALSE
(144 list items, each with ~ 23 million items). I want to combine them using any
to create one boolean vector. If any of the first elements of each element of the list TRUE
, are returned TRUE
, etc. For the length of the vectors. Here's an example:
# Some data
set.seed(1)
ll <- replicate(3,sample(c(TRUE,FALSE),5,TRUE),simplify=F)
#[[1]]
#[1] TRUE TRUE FALSE FALSE TRUE
#[[2]]
#[1] FALSE FALSE FALSE FALSE TRUE
#[[3]]
#[1] TRUE TRUE FALSE TRUE FALSE
# What I want (and one way of doing it)...
apply( do.call(cbind,ll) , 1 , any )
# [1] TRUE TRUE FALSE TRUE TRUE
Wait, you have already posted the solution in this code, why ask a question?
I have 144 vectors, each of which is 23,721,703 lengths in my real data. Attempting the above errors such as:
# *** caught segfault ***
#address 0x18, cause 'memory not mapped'
OR
#Error in aperm.default(X, c(s.call, s.ans)) :
# long vectors not supported yet: memory.c:1648
I am running R 3.0.2 on Ubuntu 64bit with 112GB of RAM.
+3
source to share
1 answer
you can use Reduce
Reduce('|', ll)
Benchmarks
set.seed(1)
ll <- replicate(144, sample(c(TRUE, FALSE), 1e5,
replace=TRUE), simplify=FALSE)
system.time(apply(do.call(cbind, ll), 1, any))
# user system elapsed
# 0.575 0.022 0.598
system.time(Reduce(`|`, ll))
# user system elapsed
# 0.287 0.008 0.295
+3
source to share