Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance matrix to pairwise distance list in R

Is there any R package to obtain a pairwise distance list if my input file is a distance matrix For eg, if my input is a data.frame like this:

        A1      B1      C1      D1
 A1     0      0.85    0.45    0.96 
 B1            0       0.85    0.56
 C1                    0       0.45
 D1                            0

I want the output as:

A1  B1  0.85
A1  C1  0.45
A1  D1  0.96
B1  C1  0.85
B1  D1  0.56
C1  D1  0.45

I found a question to do the opposite function using package 'reshape' but could not tweak it to get what I wanted.

like image 851
Anurag Mishra Avatar asked Jan 11 '15 21:01

Anurag Mishra


2 Answers

A couple of other options:

  1. Generate some data

    D <- dist(cbind(runif(4), runif(4)), diag=TRUE, upper=TRUE) # generate dummy data
    m <- as.matrix(D) # coerce dist object to a matrix
    dimnames(m) <- dimnames(m) <- list(LETTERS[1:4], LETTERS[1:4]) 
    
  2. Assuming you just want the distances for pairs defined by the upper triangle of the distance matrix, you can do:

    xy <- t(combn(colnames(m), 2))
    data.frame(xy, dist=m[xy])
    
    #  X1 X2      dist
    # 1 A  B 0.3157942
    # 2 A  C 0.5022090
    # 3 A  D 0.3139995
    # 4 B  C 0.1865181
    # 5 B  D 0.6297772
    # 6 C  D 0.8162084
    
  3. Alternatively, if you want distances for all pairs (in both directions):

    data.frame(col=colnames(m)[col(m)], row=rownames(m)[row(m)], dist=c(m))
    
    #    col row      dist
    # 1    A   A 0.0000000
    # 2    A   B 0.3157942
    # 3    A   C 0.5022090
    # 4    A   D 0.3139995
    # 5    B   A 0.3157942
    # 6    B   B 0.0000000
    # 7    B   C 0.1865181
    # 8    B   D 0.6297772
    # 9    C   A 0.5022090
    # 10   C   B 0.1865181
    # 11   C   C 0.0000000
    # 12   C   D 0.8162084
    # 13   D   A 0.3139995
    # 14   D   B 0.6297772
    # 15   D   C 0.8162084
    # 16   D   D 0.0000000
    

    or the following, which excludes any NA distances, but doesn't keep the column/row names (though this would be easy to rectify since we have the column/row indices):

    data.frame(which(!is.na(m), arr.ind=TRUE, useNames=FALSE), dist=c(m))
    
like image 73
jbaums Avatar answered Sep 19 '22 01:09

jbaums


If you have a data.frame you could do something like:

df <- structure(list(A1 = c(0, 0, 0, 0), B1 = c(0.85, 0, 0, 0), C1 = c(0.45, 
0.85, 0, 0), D1 = c(0.96, 0.56, 0.45, 0)), .Names = c("A1", "B1", 
"C1", "D1"), row.names = c(NA, -4L), class = "data.frame")

data.frame( t(combn(names(df),2)), dist=t(df)[lower.tri(df)] )
  X1 X2 dist
1 A1 B1 0.85
2 A1 C1 0.45
3 A1 D1 0.96
4 B1 C1 0.85
5 B1 D1 0.56
6 C1 D1 0.45

Another approach if you have it as a matrix with row+col-names is to use reshape2 directly:

mat <- structure(c(0, 0, 0, 0, 0.85, 0, 0, 0, 0.45, 0.85, 0, 0, 0.96, 
0.56, 0.45, 0), .Dim = c(4L, 4L), .Dimnames = list(c("A1", "B1", 
"C1", "D1"), c("A1", "B1", "C1", "D1")))

library(reshape2)
subset(melt(mat), value!=0)

   Var1 Var2 value
5    A1   B1  0.85
9    A1   C1  0.45
10   B1   C1  0.85
13   A1   D1  0.96
14   B1   D1  0.56
15   C1   D1  0.45
like image 29
J.R. Avatar answered Sep 22 '22 01:09

J.R.