Generate a lower triangle matrix with genetic distance

I have such a distance matrix

      1   2   3   4   5
A   0.1 0.2 0.3 0.5 0.6
B   0.7 0.8 0.9 1   1.1
C   1.2 1.3 1.4 1.5 1.6
D   1.7 1.8 1.9 2   2.1
E   2.2 2.3 2.4 2.5 2.6

and now I want to create a lower triangular matrix like this

    1   2   3   4   5   A   B   C   D   E
1   0                                   
2   0.1 0                               
3   0.2 0.1 0                           
4   0.4 0.3 0.2 0                       
5   0.5 0.4 0.3 0.1 0                   
A   0.1 0.2 0.3 0.5 0.6 0               
B   0.7 0.8 0.9 1   1.1 0.6 0           
C   1.2 1.3 1.4 1.5 1.6 1.1 0.5 0       
D   1.7 1.8 1.9 2   2.1 1.6 1   0.5 0   
E   2.2 2.3 2.4 2.5 2.6 2.1 1.5 1   0.5 0

I just subtracted the distance between 2 of 1 from the first table to get the genetic distance between 1 and 2 (0.2 - 0.1 = 0.1), and this is how I did for the rest of the entries, and I don’t know how is this done right or wrong ?, after performing the calculation, as did the lower triangle of the matrix. I tried this in R

x <- read.csv("AD2.csv", head = FALSE, sep = ",")
b<-lower.tri(b, diag = FALSE)

but I only get TRUE and FALSE, since the output does not look like a distance matrix. can anyone help solve this problem and here is a link to my sample data .

+4
source share
1 answer

dist . cbind . rbind 2 . NA, .

mat <- rbind(
    cbind(as.matrix(dist(tbl[1,])), tbl),
    cbind(tbl, as.matrix(dist(tbl[,1])))
)
mat[upper.tri(mat, diag=FALSE)] <- NA
mat

, .

:

tbl <- as.matrix(read.table(text="1   2   3   4   5
A   0.1 0.2 0.3 0.5 0.6
B   0.7 0.8 0.9 1   1.1
C   1.2 1.3 1.4 1.5 1.6
D   1.7 1.8 1.9 2   2.1
E   2.2 2.3 2.4 2.5 2.6", header=TRUE, check.names=FALSE, row.names=1))
+3

Source: https://habr.com/ru/post/1692904/


All Articles