Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created August 27, 2019 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishanretty/b9b5d2bd8bf88defa1c3dbef24ce2dae to your computer and use it in GitHub Desktop.
Save chrishanretty/b9b5d2bd8bf88defa1c3dbef24ce2dae to your computer and use it in GitHub Desktop.
Getting a grand sum of a 3d array quickly in R
### 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