Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert Bray-Curtis dissimilarity matrix into a pairwise dataframe?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading