[R] extract half a matrix

John Fox jfox at mcmaster.ca
Thu May 22 17:39:51 CEST 2003


Dear Tord,

At 04:55 PM 5/22/2003 +0200, Tord Snall wrote:

>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
>
>I want
>
>26
>49
>44
>49
>40
>21
>

If you want the result as a one-column matrix, 
as.matrix(mat[upper.tri(mat)]) will do the trick.

John


-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox




More information about the R-help mailing list