Question: Applying a function to a matrix
0
gravatar for Za
5 weeks ago by
Za40
Za40 wrote:

Hi, I have a matrix in which my columns are my samples and rows my genes. How I can write a function in R so that each value in this matrix be e^value?? I mean anti natural log of the values inside this matrix. Whatever I am googling I failed especially I am not allowed to ask in stack overflow.

rna-seq R • 184 views
ADD COMMENTlink modified 5 weeks ago by Jean-Karim Heriche16k • written 5 weeks ago by Za40
2
gravatar for Kevin Blighe
5 weeks ago by
Kevin Blighe24k
USA / Europe / Brazil
Kevin Blighe24k wrote:

Wait, on which scale are your values currently? Already logged or not?

You literally just want e^value? Then, that's just

2.71828182846^datamatrix

...or

exp(datamatrix)
ADD COMMENTlink written 5 weeks ago by Kevin Blighe24k

Data is already natural log transformed. I want anti natural log

ADD REPLYlink written 5 weeks ago by Za40
1

If they are the natural logged values, then, yes, just use either of:

2.71828182846^datamatrix

exp(datamatrix)

Then, they will be back on the scale they were before natural log transformation, i.e., anti natural log.

Testing:

2.71828182846^log(2)
[1] 2

exp(log(2))
[1] 2
ADD REPLYlink written 5 weeks ago by Kevin Blighe24k
1
gravatar for Jean-Karim Heriche
5 weeks ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche16k wrote:

Reversing a log is called exponentiation. To apply a function to every cell of a matrix, you can use the apply() function specifying both rows and columns in the margin parameter. The example below reverses the log2 transformation used for example in microarray data:

 exponentiated.data <- apply(log.data, 1:2, function(x) { 2^x})
ADD COMMENTlink written 5 weeks ago by Jean-Karim Heriche16k
3

Umm, 2^log.data is a bit simpler. Of course since OP wants the natural log exp(log.data) would be the equivalent.

ADD REPLYlink modified 5 weeks ago • written 5 weeks ago by Devon Ryan81k
1

Absolutely no reason to use apply in this case, simple 2^log.data is about 200x faster. (Tested with 1000 by 1000 matrix).

ADD REPLYlink modified 5 weeks ago • written 5 weeks ago by zx87544.7k

Sorry, for example in my natural log transformed matrix for a gene I have 0.0264033375579175, by your function e^x if e= 2.718281828459, now I have 1.026760. Is it right?

ADD REPLYlink written 5 weeks ago by Za40
1

Yes / Sim / Sea / Oui / Si

ADD REPLYlink written 5 weeks ago by Kevin Blighe24k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1468 users visited in the last hour