Elizabeth
Elizabeth

Reputation: 6561

Returning both halves of a distance matrix in R

I have an external distance object (gdis) and need to convert it to a matrix which contains both halves of the distance matrix. I am currently using the print function to achieve this. It is rather clunky having to print out the entire matrix each time a run the script. Is there any way to avoid this?

wgdis<-print(gdis, upper=TRUE)

Upvotes: 1

Views: 149

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

m <- matrix(rpois(50,5), nrow=5)
m2 <- dist(m)
attr(m2, "Upper") <- TRUE
m2
          1         2         3         4         5
1           12.083046 11.135529 11.958261 12.529964
2 12.083046            6.928203 10.148892  8.062258
3 11.135529  6.928203            8.660254  9.643651
4 11.958261 10.148892  8.660254           14.491377
5 12.529964  8.062258  9.643651 14.491377   

Upvotes: 3

Related Questions