Thanks a lot, with usedist::dist_make()
I was able to produce the intended solution.
After generating the class "dist" matrix calling phyloseq::distance()
, I extracted the grouping variables from the phyloseq object with:
group2samp <- list()
group_list <- get_variable(sample_data(physeq), group)
for (groups in levels(group_list)) { # loop over the no. of group levels
target_group <- which(group_list == groups)
group2samp[[ groups ]] <- sample_names(physeq)[target_group]
}
Then I melted the resulting "group2samp" list and rearranged the order of the first column to match with my distance matrix:
library(reshape2)
item_groups = melt(group2samp)
library(dplyr)
item_groups = arrange(item_groups, value)
# needed to reverse the column to match with my distance matrix
item_groups = item_groups[order(nrow(item_groups):1),]
item_groups = item_groups$L1 #extract only grouping variable
library(usedist)
distances = dist_groups(distance_matrix, item_groups)
distances
Item1 Item2 Group1 Group2 Label Distance
1 sample9 sample8 Patch Plaque Between Patch and Plaque 0.94344640
2 sample9 sample70 Patch nonlesional Between nonlesional and Patch 0.60253312
3 sample9 sample69 Patch Patch Within Patch 0.62086228