I hope to obtain the percentage of rows with a maximum value of 1 in a 4-dimensional array. I can obtain that information using an apply function for individual values of the 4th dimension, then use cbind to combine all of the output. However, the number of values in the 4th dimension may vary. Is there an efficient way to obtain the output in the desired format without using cbind?
Below is functional code that returns the desired information as separate vectors for each level of the 4th dimension of an array.
set.seed(9345)
A <- 8
B <- 2
C <- 5
D <- 2 # the value of D might change
y2 <- array(rbinom(A*B*C*D, 1, 0.4), dim = c(A, B, C, D))
pA <- colMeans(apply(y2[,,,1], c(1,3), max))
pB <- colMeans(apply(y2[,,,2], c(1,3), max))
pp <- cbind(pA, pB)
pp
# pA pB
# [1,] 0.750 0.875
# [2,] 0.625 0.250
# [3,] 0.375 1.000
# [4,] 0.375 0.500
# [5,] 0.625 0.750
Is there an easy way to obtain the equivalent of pp without using cbind on the output pA and pB (maybe a one-liner that incorporates the apply and colMeans functions)? If the number of values in the 4th dimension (D) change then the number of vectors that must be combined with cbind would vary.
Thank you for any suggestions.
y, but then operate ony2.) If you doy2 <- y, then the resultant matrix isn't as shown. (It instead matches the one in my answer below).