# [R] extracting diagonal from dist()

Thu Oct 2 12:25:53 CEST 2008

```Stephen, I think this is what you want:

> diag(as.matrix(d)[-1, ])

the '-1' above means that you ignore row one in the matrix. Given that
it's a square matrix, I suppose you could also also do:

> diag(as.matrix(d)[-1,-19])

the '-19' above mean you ignore the 19th column of hte matrix. Both
give the same answer on my machine.

Hope that helps a little,
Tony

Ps. This is my first attempt at helping someone in this group, so yay
for me!  :-)

stephen sefick wrote:
> h <- structure(list(V1 = c(-0.351714766, 0.188298251, 0.042951816,
> -0.072490327, -0.691885485, -0.816169763, -0.7066502, -0.856286332,
> -0.839723411, -0.427242353, -0.372911996, 0.326707494, 0.07847893,
> 0.687447841, 0.516105863, 0.267076547, 0.727867663, 0.432699191,
> 0.258610632), V2 = c(0.256636068, -0.824072121, -0.149185618,
> 0.153280492, 0.01649528, 0.409410528, 0.286015324, 0.366323539,
> 0.426800995, -0.735712725, -0.767824938, -0.440086289, -0.308010116,
> 0.458001784, 0.592207506, 0.449511677, -0.007212198, -0.046479998,
> -0.19965154), V3 = c(0.033986839, 0.128179407, 0.525683422, 0.330851415,
> 0.800996941, 0.031813218, -0.232375584, -0.261745661, -0.446943818,
> -0.067349584, -0.210142202, 0.288245114, -0.426273652, 0.179351629,
> 0.299167413, -0.45663776, -0.172400654, -0.273902898, -0.393874281
> ), V4 = c(0.65461555, -0.046146226, 0.355091147, 0.201657295,
> -0.032329184, -0.414280507, -0.466305336, -0.207174265, -0.2168161,
> -0.190256903, -0.087694801, -0.366977945, 0.153600027, -0.253544176,
> -0.279933607, 0.323432824, -0.272048254, -0.313094051, -0.033307846
> )), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame",
> row.names = c("1201 dn",
> "0502 dn", "0702 dn", "1002 dn", "1102 dn", "1202 dn", "0103 dn",
> "0203 dn", "0303 dn", "0403 dn", "0503 dn", "0803 dn", "0104 dn",
> "0704 dn", "0804 dn", "1204 dn", "0805 dn", "1005 dn", "0106 dn"
> ))
>
> d <- dist(h)
> diag(as.matrix(d))
>
>
> this is close I just need the diagonal to start one more row down.  In
> other words I just want the diagonal starting below the 0 diagonal.  I
> am having a hard time figuring this one out..
>
> --
> Stephen Sefick
> Research Scientist
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods.  We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> 								-K. Mullis
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help