extracting a matrix of genes from another bigger matrix
1
0
Entering edit mode
9.5 years ago
zizigolu ★ 4.4k

hi,

i have two symetric matrics of genes like below

> mat <- read.table("aracne-tmatnorm_rld500.txt", header = T, sep = "\t", row.names=1)
> head(mat[,1:4])
          AT1G01060 AT1G01170 AT1G01180 AT1G01260
AT1G01060         0         0         0         0
AT1G01170         0         0         0         0
AT1G01180         0         0         0         0
AT1G01260         0         0         0         0
AT1G01380         0         0         0         0
AT1G01490         0         0         0         0
> # watching the dimension of matrix
> dim(mat)
[1] 3123 3123

> Newmat <- read.table("newMat.txt", header = T, sep = "\t", row.names=1)
> head(Newmat[,1:4])
          AT1G01060 AT1G01170 AT1G01180 AT1G01183
AT1G01060         0         0         0         0
AT1G01170         0         0         0         0
AT1G01180         0         0         0         0
AT1G01183         0         0         0         0
AT1G01260         0         0         0         0
AT1G01380         0         0         0         0
> # watching the dimension of matrix
> dim(Newmat)
[1] 3515 3515

how i can extract one matrix from another please??

i tried

> data1_subset <- Newmat[ Newmat$row.name %in% mat$row.name, ]
Error in mycounts1$row.name : $ operator is invalid for atomic vectors
> data1_merge <- merge(mat, Newmat, by = "row.name")
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
> data1_merge <- merge(mat, Newmat, by = "col.name")
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
> Common <- merge(mat, Newmat, by.x=c("colNameFrom-mycounts"), by.y=c("colNameFrom-mycounts1"))
Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

thank you

gene R • 3.2k views
ADD COMMENT
2
Entering edit mode
9.5 years ago

Do something like this to select the columns:

index<-colnames(mat) %in% colnames(Newmat)
selected<-mat[,index]

Repeat for the rows.

ADD COMMENT
0
Entering edit mode

sorry i need to make the bigger matrix to dimension

3123 3123

as the small matrix,

ADD REPLY
0
Entering edit mode

You only asked how to extract a matrix from another so I gave you some generic code. Swap mat and Newmat in the code to get columns from Newmat that are also in mat.

ADD REPLY
0
Entering edit mode

sorry, i mean i have a 3123 X 3123 matrix and a 3515 X 3515. then how i can extract only a 3123 X 3123 matrix from the 3515 X 3515 matrix?

ADD REPLY
0
Entering edit mode

I assumed you wanted the rows and columns of one matrix to match that of the other one. Now if you don't care about the content then just resize the matrix.

ADD REPLY
0
Entering edit mode

actually the content of these files are not the same. I have two adjacency matrices a 3123 3123 matrix and a 3515 3515. rownames and colnames are the gene IDs and the same in both matrices but one is bigger I need both matrices with the same dimension and same variable (gene IDs) but I don't know how to extract only the small matrix gene IDs from the big one with conserving the content. In brief I need both matrices but with the same dimension and variable but of course the content is not the same.

thank you

ADD REPLY
1
Entering edit mode

The answer I gave is doing what you want. To reiterate with comments:

index<-colnames(Newmat) %in% colnames(mat) # Find column variables of Newmat that are also in mat
selected<-Newmat[,index] # Extract columns of Newmat that match those in mat

Now do the same for the rows.

ADD REPLY
0
Entering edit mode

thank you

i did like below

> index<-colnames(Newmat) %in% colnames(mat)
> selected<-Newmat[,index]
> dim(selected)
[1] 3515 3123
> index<-rownames(selected) %in% rownames(mat)
> Newmat<-selected[,index]
Error in selected[, index] : (subscript) logical subscript too long
ADD REPLY
1
Entering edit mode

You're now dealing with the rows so you need to also switch the position of index i.e.

Newmat<-selected[index,] # Select rows
ADD REPLY
0
Entering edit mode

thank you very much for assigning your precious time and also your patience

ADD REPLY

Login before adding your answer.

Traffic: 2463 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