Reputation: 993
I have two character subarrays as follows:
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] "5" "3" "1" "1" "5"
[2,] "4" "1" "1" "1" "3"
[3,] "5" "5" "4" "5" "1"
[4,] "5" "5" "1" "3" "5"
[5,] "3" "4" "1" "4" "5"
[6,] "2" "5" "4" "5" "2"
[7,] "2" "5" "5" "4" "2"
[8,] "5" "5" "2" "2" "2"
[9,] "2" "5" "4" "5" "4"
[10,] "2" "2" "5" "2" "1"
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] "2" "2" "2" "4" "5"
[2,] "3" "1" "2" "1" "4"
[3,] "5" "2" "3" "4" "4"
[4,] "3" "3" "4" "5" "3"
[5,] "2" "1" "5" "1" "3"
[6,] "5" "5" "2" "5" "4"
[7,] "1" "5" "1" "3" "4"
[8,] "1" "5" "3" "3" "5"
[9,] "4" "5" "1" "1" "2"
[10,] "5" "1" "2" "3" "5"
Now I have written an R routine to randomly sample a row from ONE of these arrays (either ,,1 or ,,2).
But what I would instead like to do is sample randomly from BOTH subarrays and take the column-wise mean. Can I accomplish this without converting from character to numeric values first?
So, if row 1 is sampled in both ,,1 and ,,2, the columnwise mean (rounded appropriately) would be (not done in R)
"(5+2)/2", "(3+2)/2", "(1+2)/2", "(1+4)/2", "(5+5)/2"
= "4", "3", "2", "3", "5"
My (partial) routine for the random sampling of a row from ONE array is below. I want to modify it for TWO arrays.
for(k in specs){
for(j in 1:perms){
for(i in 1:K){
ind.index <- sample(specs, size = k, replace = FALSE)
hap.plot <- pop[sample(1:nrow(pop), size = 1, replace = TRUE), ind.index, sample(i, size = 1, replace = TRUE)]
HAC.mat[j, k, i] <- length(unique(hap.plot))
}
}
}
Any assistance is greatly appreciate.
Please let me know if more clarification is required.
Upvotes: 0
Views: 58
Reputation: 887421
We can use apply
i1 <- sample(seq_len(dim(ar1)[1]), 2)
apply(ar1[i1,,], c(1, 2), FUN = function(x) mean(as.numeric(x)))
Or use rowMeans
by looping through the third dimension
rowMeans(apply(ar1[i1,,], 3, FUN = function(x) as.numeric(x)))
set.seed(24)
ar1 <- array(as.character(sample(1:5, 10*5*2, replace = TRUE)), dim = c(10, 5, 2))
Upvotes: 1
Reputation: 6222
Test data
mat <- array(as.character(1:40), c(15, 5, 2))
Randomly sample from each separately and get the mean.
n_row <- dim(mat)[1]
s_rows <- sample(1:n_row, 2, replace = TRUE)
unlist(lapply(mat[s_rows[1],, 1], as.numeric))/2 +
unlist(lapply(mat[s_rows[2],, 2], as.numeric))/2
Upvotes: 1