Home > Back-end >  How to convert Bray-Curtis dissimilarity matrix into a pairwise dataframe?
How to convert Bray-Curtis dissimilarity matrix into a pairwise dataframe?

Time:04-27

I have a Bray-Curtis dataframe in R for 162 samples, but I want to convert it into a pairwise dataframe so that column 1 has the first site, and column 2 is the second site being compared, and column 3 is the Bray-Curtis dissimilarity value. I use the vegdist command and it seems setting upper=TRUE is not working as it continues to return the entire dataframe for myself.

The actual code I use to get my BrayCurtis dataframe:

df <- vegdist(my_data, method="bray", upper=TRUE) # upper is not working, returning everything
df <- as.data.frame(as.matrix(df))

I am not great at converting or even creating dataframes in R, so any suggestions or tools for this would help. The BrayCurtis dataframe I have has rows and column headers as the sites, but I need to change it into a pairwise dataframe.

Example of my data:

    A   B   C   D
A   0   2   1   1
B   2   0   2   1
C   1   2   0   1
D   1   1   1   0

What I want as output is the upper diagonal values but as a pairwise table:

A   B   2
A   C   1
A   D   1
B   C   2
B   D   1
C   D   1

Thank you!

CodePudding user response:

in Base R:

as.data.frame.table(as.matrix(df))|>
  transform(Var1 = as.character(Var1), Var2 = as.character(Var2)) |>
  subset(Var1<Var2)

  Var1 Var2 Freq
5     A    B    2
9     A    C    1
10    B    C    2
13    A    D    1
14    B    D    1
15    C    D    1

Ar even:

idx <- lower.tri(df)
cbind(rev(expand.grid(dimnames(df)))[which(idx),], val = df[idx])

   Var2 Var1 df[idx]
2     A    B       2
3     A    C       1
4     A    D       1
7     B    C       2
8     B    D       1
12    C    D       1
  •  Tags:  
  • r
  • Related