user3306125
user3306125

Reputation: 215

How to get values from correlation matrix in R?

I've made a correlation in R, and i need to exclude values from correlation matrix that less then abs (0.6) and equal 1 (main diagonal).

Is there any ways to do it?

Here is example of this matrix:

 1.00000    0.00685 0.01114 0.80120
 0.00685    1.00000 0.09668 0.05184
 0.01114    0.96680 1.00000 0.48547
-0.60120    0.05184 0.48547 1.00000

And it might be looking like this:

                            0.80120

            0.96680         
-0.60120    

Upvotes: 0

Views: 164

Answers (2)

Silence Dogood
Silence Dogood

Reputation: 3597

you can try:

mat <- as.matrix(read.table(tc <- textConnection(
"1.00000    0.00685 0.01114 0.80120
0.00685    1.00000 0.09668 0.05184
0.01114    0.96680 1.00000 0.48547
-0.60120    0.05184 0.48547 1.00000"), header = FALSE),nrow=4); 

close(tc)


mat[abs(mat) < 0.6 | abs(mat) == 1]<-""

noquote(mat)

> noquote(mat)
 V1      V2     V3 V4    
[1,]                   0.8012
[2,]                         
[3,]         0.9668          
[4,] -0.6012

Note that since you wish to have blank/empty entries for your conditions, the matrix elements will be coerced to character type.

Upvotes: 0

Roman Luštrik
Roman Luštrik

Reputation: 70623

How about if you set values that meet a criteria to NA?

> set.seed(357)
> x <- matrix(rnorm(16), ncol = 4)
> x <- cor(x)
> x[x > 0.25] <- NA
> print(x, 3)
       [,1]   [,2]   [,3]   [,4]
[1,]  0.000 -0.668 -0.263 -0.615
[2,] -0.668  0.000  0.000  0.000
[3,] -0.263  0.000  0.000 -0.516
[4,] -0.615  0.000 -0.516  0.000

Upvotes: 1

Related Questions