# fake case
library(multiApply)
# I imagin that you have, for each region, several ensembles. 

a <- rnorm(300 * 31 * 3)
dim(a) <- c(region = 31, datasets = 3, memb = 300)

# For a given region and dataset, you want to apply bootstrapping to choose several combinationsand compute your statistics.

#---------------------------------
# a) we don't worry to use a memb more than once (with replacement)
# the size of my sample can be any... take 100

pos <- sample(1:300, 100*1000, replace = TRUE)
dim(pos) <- c(size = 100, nboot = 1000)

# Now I want to compute my statistics, which is the mean to simplify.

library(multiApply)
MyMean <- function(x, y) {mean(x[y])}
res <- Apply(list(a, pos), target_dims = list('memb', 'size'), fun = MyMean)
dim(res$output1)

mean(res$output1[1,1,]) # around zero


#---------------------------------
# b) we worry to use a memb more than once (without replacement)
# the size of my sample is resticted:
# cannot take a sample larger than the population when 'replace = FALSE' 

pos <- unlist(lapply(1:1000, function(x) {sample(1:300, 100, replace = FALSE)}))
dim(pos) <- c(size = 100, nboots = 1000)
# check repeated by rows
apply(apply(pos, 2, duplicated), 2, any) #no duplicates

# Now I want to compute my statistics, which is the mean to simplify.

res <- Apply(list(a, pos), target_dims = list('memb', 'size'), fun = MyMean)
dim(res$output1)

mean(res$output1[1,1,]) # around zero


