[R] extract half a matrix

Thomas Lumley tlumley at u.washington.edu
Thu May 22 17:45:21 CEST 2003


On Thu, 22 May 2003, Tord Snall wrote:

> Dear all,
>
> I'm new to matrix operations in R. I couln't find a solution to the
> following problem among earlier help mails or in An introd to R, I guess
> because the question is really basic.
>
> I want to extract all above the diagonal, i.e. from
>
>     1  2  3  4
> 1   0 26 49 49
> 2  26  0 44 40
> 3  49 44  0 21
> 4  49 40 21  0
>

In addition to the solution suggested by other people there are built-in
functions upper.tri and lower.tri for doing this sort of thing, so

  x[upper.tri(x)]


	-thomas




More information about the R-help mailing list