I have a large list of TRUE/FALSE
logical vectors (144 list elements, each ~ 23 million elements long). I want to merge them using any
to produce one logical vector. If any of the first elements of each list element are TRUE
then TRUE
is returned and so on for the length of the vectors. Here's an example:
我有大量的真/假逻辑向量(144个列表元素,每一个~ 2300万个元素)。我想用any合并它们来产生一个逻辑向量。如果每个列表元素的第一个元素都为真,则返回TRUE,以此类推。这里有一个例子:
# 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 already posted a solution in that code, why ask the question?
I have 144 vectors, each of 23,721,703 length in my real data. Attempting the above throws errors such as:
我有144个向量,在我的真实数据中,每个向量的长度都是23721703。尝试上述操作会产生如下错误:
# *** 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'm running R 3.0.2 on Ubuntu 64bit with 112Gb RAM.
我在ubuntu64bit上运行r3.0.2,带有112Gb的RAM。
1 个解决方案
#1
3
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
#1
3
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