Created
August 27, 2019 17:41
-
-
Save chrishanretty/b9b5d2bd8bf88defa1c3dbef24ce2dae to your computer and use it in GitHub Desktop.
Getting a grand sum of a 3d array quickly in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This generates an array which takes up around 600 Mb in memory | |
input_sizes <- c(100, .1e6, 8) | |
names(input_sizes) <- c("draws", "nobs", "categories") | |
my_arr <- array(rnorm(prod(input_sizes)), | |
dim = input_sizes) | |
## Returns a 2D matrix of dimensions draws by nobs | |
## i.e., sums over categories | |
### This version takes around 17-18s on my laptop | |
system.time(grand_total_a <- apply(my_arr[,,], c(1, 2), sum)) | |
### This version takes about 2-3 seconds | |
system.time({ | |
grand_total_b <- matrix(NA, nrow = 100, ncol = .1e6) | |
for (i in 1:100) { | |
grand_total_b[i, ] <- rowSums(my_arr[i, , ]) | |
} | |
}) | |
all(grand_total_a == grand_total_b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment