Suppose i have a logical array :
dimensions = c(10,100,200,300)
x = runif(prod(dimensions))>0.3
dim(x) = dimensions
Is there a faster way to get the result :
y = colMeans(apply(x,2:4,all))
?
Apply is quite slow, and this use case is quite straightforward, hence i wander...
Use colSums
:
system.time(
y <- colMeans(apply(x,2:4,all))
)
# user system elapsed
#6.916 0.058 6.978
system.time(
z <- colMeans(colSums(x, dims = 1) == dim(x)[1])
)
# user system elapsed
#0.117 0.000 0.117
identical(y, z)
#[1] TRUE