# [R] as.dist with diagonal unequal zero

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Oct 10 17:18:11 CEST 2007

```On Wed, 2007-10-10 at 16:42 +0200, Birgit Lemcke wrote:
> Hello and sorry that I still haven´t found a solution for my problem.
>
> I need to extract the lower and upper triangle from a square matrix
> including the diagonal. This diagonal is not zero in that special case.

How can a site have a dissimilarity to itself greater than 0, when
assessed on the same suite of variables?

Even if you think it is valid, I don't see a way using objects of class
"dist" as these don't even store the diagonal values, just the
off-diagonal values, so there is nothing to replace after you've
as.dist'd your matrix with the real diagonal values.

As you say you need objects of class "dist" for some subsequent
analysis, I think you are out of luck. But consider that this other
analysis is likely to be assuming that the diagonals are 0, so even if
you did find a way to do what you wanted, the results might not mean
anything useful.

G

> I tried with as.dist
>
> w<-as.dist(w, diag = TRUE)
>  > w
>     1  2  3  4  5
> 1  0
> 2  2  0
> 3  3  8  0
> 4  4  9 14  0
> 5  5 10 15 20  0
>
> but found no way to keep the diagonal that is in the matrix.
>
> I also tried this but as I expected it makes no difference:
>
> w<-matrix(1:25, ncol=5, nrow=5)
>  > upperTriangle(w)<-NA
>  > w
>       [,1] [,2] [,3] [,4] [,5]
> [1,]    1   NA   NA   NA   NA
> [2,]    2    7   NA   NA   NA
> [3,]    3    8   13   NA   NA
> [4,]    4    9   14   19   NA
> [5,]    5   10   15   20   25
>  > w<-as.dist(w, diag = TRUE)
>  > w
>     1  2  3  4  5
> 1  0
> 2  2  0
> 3  3  8  0
> 4  4  9 14  0
> 5  5 10 15 20  0
>
> Is there perhaps a possibility to give the diagonal as a vector
> within as.dist?
>
> I would be glad if somebody could help me.
>
> Greetings
>
> Birgit
>
> Birgit Lemcke
> Institut für Systematische Botanik
> Zollikerstrasse 107
> CH-8008 Zürich
> Switzerland
> Ph: +41 (0)44 634 8351
> birgit.lemcke at systbot.uzh.ch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                 [t] +44 (0)20 7679 0522
ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```