[R] as.dist with diagonal unequal zero

Gustaf Rydevik gustaf.rydevik at gmail.com
Wed Oct 10 16:59:14 CEST 2007


On 10/10/07, Birgit Lemcke <birgit.lemcke at systbot.uzh.ch> 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.
> 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
>

What do you want to do with the lower triangle matrix?
A distance matrix must be zero on the diagonal, since dist(a,a)==0 for
all a, so it cannot be distances that you measure.

best,
Gustaf


-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list