Entering edit mode
8.2 years ago
zizigolu
★
4.3k
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
sorry i need to make the bigger matrix to dimension
as the small matrix,
You only asked how to extract a matrix from another so I gave you some generic code. Swap
mat
andNewmat
in the code to get columns fromNewmat
that are also inmat
.sorry, i mean i have a
3123 X 3123
matrix and a3515 X 3515
. then how i can extract only a3123 X 3123
matrix from the3515 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:
Now do the same for the rows.
thank you
i did like below
You're now dealing with the rows so you need to also switch the position of index i.e.
thank you very much for assigning your precious time and also your patience