Is there a way to assign vector elements to multiple subarrays in R, using sample() or split() (or a combination of both functions)?
Essentially what I need is a function that randomly assigns values to multiple subarrays
Here's my full specific code:
K <- 2 # number of subarrays
N <- 100
Hstar <- 10
perms <- 10000
probs <- rep(1/Hstar, Hstar)
K1 <- c(1:5)
K2 <- c(6:10)
specs <- 1:N
pop <- array(dim = c(c(perms, N), K))
haps <- as.character(1:Hstar)
for(j in 1:perms){
for(i in 1:K){
if(i == 1){
pop[j, specs, i] <- sample(haps, size = N, replace = TRUE, prob = probs)
} else{
pop[j,, 1] <- sample(haps[K1], size = N, replace = TRUE, prob = probs[K1])
pop[j,, 2] <- sample(haps[K2], size = N, replace = TRUE, prob = probs[K2])
}
}
}
pop[j,, 1] is the first subarray in pop, while pop[j,, 2] is the second subarray in pop
If I have 20 subarrays, using sample() 20 times is tedious. I just want a way to assign values to the any number of subarrays quickly and easily.
Any ideas?