sorry i need to make the bigger matrix to dimension
3123 3123
as the small matrix,
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
Do something like this to select the columns:
index<-colnames(mat) %in% colnames(Newmat)
selected<-mat[,index]
Repeat for the rows.
sorry i need to make the bigger matrix to dimension
3123 3123
as the small matrix,
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.
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?
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.
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
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.
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
Log in to answer this question.