[R] Printing row and column names of cells with specific value in a big matrix

gaurav kandoi kandoigaurav at gmail.com
Mon Jul 20 21:29:04 CEST 2015


Hi Sarah, sorry for posting in HTML.

I've two big matrices (5k*4k) with the same structure, i.e. :

,mRNA1,mRNA2,mRNA3
lncRNA1,0.395646498,0.949950035,0.761770206
lncRNA2,0.037909944,0.661258022,0.558657799
lncRNA3,0.678459646,0.652364052,0.359053653

Now, I would like to extract the names of the row,col pairs whose
value is less than 0.05. In this case, I should get the output as
(lncRNA2,mRNA1) and (lncRNA4,mRNA2) alongwith their values (0.03791
and 0.003). Since the structure of both the matrix is same, I would
also like to retrieve the corresponding values and row,col names from
the second matrix. (lncRNA2,mRNA1 and lncRNA4,mRNA2 alongwith their
values in the second matrix.)

I'm using the following code:

> Pmatrix = read.table("pmatrix.csv", header=T, sep="," , row.names=1)
> sig_values <- which(Pmatrix<0.05, arr.ind=TRUE)
> sig_values
> Corr_Matrix = read.csv("corr_matrix.csv", header = T, row.names=1)
> Corr_Matrix[sig_values]

However, it only prints the row,col number (sig_values command) or
only the values (Corr_Matrix[sig_values]) command. How can I get the
row and column names alongwith their values?

I've also tried printing using the following print command:

>paste(rownames(Pmatrix)[sig_values[1]], colnames(Pmatrix)[sig_values[2]], sep=", ")

But it gives a output like this:

[1] "lncRNA2, NA"


Sample input files available for download: https://goo.gl/xR6XDg

Regards

On Mon, Jul 20, 2015 at 2:11 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> Without a reproducible example, or at least a non-mangled one (please
> don't post in HTML), I'm not inclined to try it, but why not use
> sig_values to index row.names() and col.names() if you're after the
> names?
>
> Sarah
>
> On Mon, Jul 20, 2015 at 1:44 PM, gaurav kandoi <kandoigaurav at gmail.com> wrote:
>> Hi All
>>
>> I've two big matrices (5k*4k) with the same structure, i.e. :
>>
>>   mRNA1 mRNA2 mRNA3  lncRNA1 0.395646 0.94995 0.76177  lncRNA2 0.03791
>> 0.661258 0.558658  lncRNA3 0.67846 0.652364 0.359054  lncRNA4 0.57769 0.003
>> 0.459127
>> Now, I would like to extract the names of the row,col pairs whose value is
>> less than 0.05. In this case, I should get the output as (lncRNA2,mRNA1)
>> and (lncRNA4,mRNA2) alongwith their values (0.03791 and 0.003). Since the
>> structure of both the matrix is same, I would also like to retrieve the
>> corresponding values and row,col names from the second matrix.
>> (lncRNA2,mRNA1 and lncRNA4,mRNA2 alongwith their values in the second
>> matrix.)
>>
>> I'm using the following code:
>>
>> Pmatrix = read.table("pmatrix.csv", header=T, sep="," , row.names=1)
>>> sig_values <- which(Pmatrix<0.05, arr.ind=TRUE)
>>> sig_values
>>> Corr_Matrix = read.csv("corr_matrix.csv", header = T, row.names=1)
>>> Corr_Matrix[sig_values]
>>
>>
>> However, it only prints the row,col number (sig_values command) or only the
>> values (Corr_Matrix[sig_values]) command. How can I get the row and column
>> names alongwith their values?
>>
>> Regards
>>
>> --
>> *Gaurav Kandoi*
>>
>>         [[alternative HTML version deleted]]
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org



-- 
Gaurav Kandoi



More information about the R-help mailing list