Question: (Closed) Extracting row and column names from matrix in R.
0
kandoigaurav • 150 wrote:
I've two large matrices with p-values and correlation coefficients for lncRNA vs mRNA genes (both matrix have same structure, one has p-values while the other has corresponding correlation coefficients). I would like to extract all lncRNA-mRNA gene pairs for which the p-value is less than 0.05 and their corresponding correlation coefficients as well.
I'm using the following R script which gives me the row and column number, but I also want the names, i.e lncRNA and mRNA gene names:
Pmatrix = read.csv ("pmatrix.csv", header = FALSE) ##read the matrix with pvalues sig_values <- which(Pmatrix<0.05, arr.in=TRUE) ##extract values less than 0.05 sig_values #list all (row,col) pairs with pvalues less than 0.05 Corr_Matrix = read.csv("corr_matrix.csv", header = FALSE) ##read the matrix with correlation values Corr_Matrix[sig_values] ##list correlation values corresponding to pvalues less than 0.05.
This gives me a list of row and column numbers, but how can I get the names?
This is not really a bioinformatics question, it can be reduced to the generic R question: Extracting row and column names from matrix in R. (as you wrote yourself). Therefore it might be closed. Please read: ?rownames ?colnames. However you won't have any sensible col names, yours will be V1, V2, ... because of header=FALSE, and you won't have row.names either, because they were not defined during import, see ?read.csv and argument row.names.
In addition, the returned value of read.table is a data.frame not a matrix, as your variable names imply.
If you read https://cran.r-project.org/doc/manuals/r-release/R-data.html#Variations-on-read_002etable you should be prepared for these basic tasks.
Hello kandoigaurav!
We believe that this post does not fit the main topic of this site.
Decided to close because it is a generic R question.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!