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