0

So far I've tried all what I have found in internet. I have a distance matrix (dist class) and all the elements I've learned to "coerce" the matrix have not been effective. I want to keep the triangular form.

Does any one already know how to export a dist matrix? f

nghauran
  • 6,648
  • 2
  • 20
  • 29
Hazlo Bob
  • 31
  • 7
  • 1
    I've been poorly qualified since I asked the same question. But, I found the answer I needed, at last! So, I want to know, if any of the qualifiers tried and worked for them the supposed answers there have been already written. – Hazlo Bob Oct 23 '17 at 03:32
  • I know this is an older question, but the people who originally answered said it was a duplicate question when it wasn't. The question that they are referring to pertains to exporting the distance matrix in non-triangular format, when this one pertains to exporting the matrix in triangular format. Another answer to this question would be: – amwalker Nov 20 '19 at 00:38
  • sink("distance_matrixt.txt") print(distance_matrix) sink() – amwalker Nov 20 '19 at 00:40

1 Answers1

1

One option is to set a new matrix from your dist matrix and then replace the upper triangular part with "". Here is an example:

df <- data.frame(x = rnorm(10, 5, 1), y = rnorm(10))
mat <- dist(df, diag = TRUE, upper = FALSE)
mat2 <- as.matrix(mat)
mat2[upper.tri(mat2, diag = FALSE)] <- ""
write.csv(mat2, "mat2.csv")

PS: the consequence of that procedure is that your matrix is now a character matrix. So, do not forget to change the class of its components before using them.

Hope it will help.

nghauran
  • 6,648
  • 2
  • 20
  • 29