非常感谢,usedist::dist_make()
我能够产生预期的解决方案。
在生成类“dist”矩阵调用phyloseq::distance()
后,我从 phyloseq 对象中提取了分组变量:
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]
}
然后我融化了生成的“group2samp”列表并重新排列了第一列的顺序以匹配我的距离矩阵:
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