简体   繁体   English

计算矩阵中匹配样本的最小距离

[英]Calculate minimum distance in matching samples in a matrix

I have a large distance matrix and a corresponding dataframe, a miniature example is : 我有一个大距离矩阵和一个对应的数据框,一个微型例子是:

A = matrix( c(0, 1, 2, 1, 0, 2, 2, 2, 0), nrow=3, ncol=3, byrow = TRUE)        
dimnames(A) = list(c("A1", "B1", "C1"), c("A1", "B1", "C1"))
df <- data.frame("ID" = c("A1", "B1", "C1"), "Triplicate" = c("T1", "T1", "T1"))

A1, B1, C1 are technical replicates of each other as indicated by identical value in Triplicate column in the df . A1, B1, C1是彼此的技术复制,如df Triplicate列中的相同值所示。 Matrix A indicates "distance or dissimilarity" of samples from one another. 矩阵A指示样本彼此之间的“距离或相异性”。 How can I group the matrix so that I append a column to df such that for any sample it is: 如何对矩阵进行分组,以便在df附加一列,以便对任何样本都可以:

a. 一种。 the minimum of the two distances from its corresponding triplicate samples. 距其对应的一式三份样本的两个距离中的最小值。 So for example, in matrix A A1:B1 distance is 1 and A1:C1 distance is 2, so append minimum of these two distance values for A1 in df column minimum as 1 and likewise minimum of distance from A1 and C1 for B1 and A1 and B1 for C1, giving me: 因此,例如,在矩阵A A1:B1距离是1,A1:C1距离是2,所以在追加这两个距离值的最小值A1 dfminimum距离的最小距离A1和C1 B1和A1为1,并且同样B1代表C1,给我:

df$minimum <- c(1, 1, 2) 
df

b. b。 Similarly, I would like to append another column average so that it is the average of two distances, so for A1 average of distances from C1 and B1 is (1+2)/2 = 1.5, similarly for B1 and C1 giving me: 类似地,我想附加另一个列平均值,以便它是两个距离的平均值,因此对于A1到C1和B1的距离的平均值是(1 + 2)/ 2 = 1.5,类似地,对于B1和C1给我:

df$average <- c(1.5, 1.5, 2)
df

Hope this is much clearer, I have many such Triplicate samples, so referring to it while matching sample distances is important. 希望这更加清楚,我有很多这样的Triplicate样本,因此在匹配样本距离时引用它很重要。 I will address any questions immediately... 我将立即解决任何问题...

Thanks! 谢谢!

How about 怎么样

df$minimum <- apply(A, 1, function(x) min(x[x > 0]))
df$average <- apply(A, 1, function(x) mean(x[x > 0]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM