I want to create a new distance matrix from an array of hundreds of distance matrices. This new matrix will be the average distances given a set of pairwise distances. The values to be averaged is based matching name prefixes. Easier to illustrate as my language might be incorrect: Here’s one sample distance matrix:
SpeciesA_1 SpeciesA_2 SpeciesC_1. SpeciesC_2
SpeciesA_1 0 0.2 0.3 0.4
SpeciesA_2 0.2 0 0.4 0.5
SpeciesC_1 0.3 0.4 0 0.1
SpeciesC_2 0.4 0.5 0.1 0
I want the mean of all possible pairwise combinations given two prefix. For example:
SpeciesA_1 & SpeciesC_1 (0.3)
SpeciesA_1 & SpeciesC_2 (0.4)
SpeciesA_2 & SpeciesC_1 (0.4)
SpeciesA_2 & SpeciesC_2 (0.5)
Therefore, my resulting new distance matrix in this example should be:
SpeciesA SpeciesC
SpeciesA 0 0.4
SpeciesC 0.4 0
My actual matrices have about 140 columns and rows. I have no idea where to even get started with this. These dists are an array:
dists[,,i]
I can’t use a loop (which I would normally start with by selecting the first name) because it is an array which I never work with.
dim(dists)
[1] 135 135 138