[R] display only the top-right half of a correlation matrix?

Liviu Andronic landronimirc at gmail.com
Fri Aug 19 21:32:41 CEST 2011


On Fri, Aug 19, 2011 at 9:02 PM, Peter Langfelder
<peter.langfelder at gmail.com> wrote:
> Use as.dist: here's an example.
>
Seems promising, but for one issue: I would like to keep the diagonal
and thus specify 'diag=T', but then as.dist() replaces the diagonal
values with zero. (See below.) Is there a way to prevent it from doing
that? Either keep the original values, or not display anything in the
diagonal (as for the upper part)?

Regards
Liviu


> (xb <- rcorr(as.matrix(mtcars[ , 1:4])))
       mpg   cyl  disp    hp
mpg   1.00 -0.85 -0.85 -0.78
cyl  -0.85  1.00  0.90  0.83
disp -0.85  0.90  1.00  0.79
hp   -0.78  0.83  0.79  1.00

n= 32


P
     mpg cyl disp hp
mpg       0   0    0
cyl   0       0    0
disp  0   0        0
hp    0   0   0
> round(as.dist(xb$r, T), 2)
       mpg   cyl  disp    hp
mpg   0.00
cyl  -0.85  0.00
disp -0.85  0.90  0.00
hp   -0.78  0.83  0.79  0.00
>



More information about the R-help mailing list