getting a matrix with diagonal =1
1
0
Entering edit mode
Angel ▴ 40
@angel-7981
Last seen 7.7 years ago
Berlin

hi,

i have a correlation matrix like below

> head(mat[,1:4])
          AT1G01060 AT1G01170 AT1G01180 AT1G01260
AT1G01060 1.0000000 0.3193360 0.6272994 0.2658624
AT1G01170 0.3193360 1.0000000 0.3178880 0.3588030
AT1G01180 0.6272994 0.3178880 1.0000000 0.2542234
AT1G01260 0.2658624 0.3588030 0.2542234 1.0000000
AT1G01380 0.6178751 0.1561297 0.7052692 0.3252033
AT1G01490 0.5990499 0.6129301 0.6424225 0.5727253
> dim(mat)
[1] 3123 3123

i did 

> mat <- diag(mat = 1, nrow, ncol = 3123)
Error in diag(mat = 1, nrow, ncol = 3123) : 
  unused argument (mat = 1)

how i can get this matrix with diagonal = 1  ???

thank you

stats • 1.6k views
ADD COMMENT
3
Entering edit mode
Aaron Lun ★ 28k
@alun
Last seen 6 hours ago
The city by the bay

This isn't really a Bioconductor question, but to answer it anyway:

diag(3123)

... will give you an identity matrix with 3123 rows/columns.

ADD COMMENT
0
Entering edit mode

thank you

i did like so but the colnames and rownames of my matrix changed

> head(mat[,1:4])
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1
[5,]    0    0    0    0
[6,]    0    0    0    0

how i can back the colnames and rownames as already please?

ADD REPLY
0
Entering edit mode

The diag function will generate an entirely new matrix. If you want the names of the old matrix, you should do:

mat[] <- diag(nrow(mat))

Or perhaps more intuitively:

mat2 <- diag(nrow(mat))
dimnames(mat2) <- dimnames(mat)

In both cases, I'm assuming that mat contains your original correlation matrix.

ADD REPLY
0
Entering edit mode

thank you

sorry no problem if the original measures from the first matrix changed in diagonal matrix to only 0 and 1???

> head(mat[,1:4])
          AT1G01060 AT1G01170 AT1G01180 AT1G01260
AT1G01060 1.0000000 0.3193360 0.6272994 0.2658624
AT1G01170 0.3193360 1.0000000 0.3178880 0.3588030
AT1G01180 0.6272994 0.3178880 1.0000000 0.2542234
AT1G01260 0.2658624 0.3588030 0.2542234 1.0000000
AT1G01380 0.6178751 0.1561297 0.7052692 0.3252033
AT1G01490 0.5990499 0.6129301 0.6424225 0.5727253

 changed to 

> head(mat[,1:4])
          AT1G01060 AT1G01170 AT1G01180 AT1G01260
AT1G01060         1         0         0         0
AT1G01170         0         1         0         0
AT1G01180         0         0         1         0
AT1G01260         0         0         0         1
AT1G01380         0         0         0         0
AT1G01490         0         0         0         0
 

>

actually i need 

a square correlation matrix with diagonal Gii = 1 
ADD REPLY
2
Entering edit mode

I don't understand your problem. From what I can see, your original matrix already has diagonals of 1. This is obviously the case, because each gene would be perfectly correlated with itself.

ADD REPLY
0
Entering edit mode

yes you all right..right now i considered my original matrix

many thanks again

ADD REPLY

Login before adding your answer.

Traffic: 588 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6