[英]How to take Log2 of a matrix having negative values for Boxplot in R
I have a matrix having three columns, there is a lot of variation in the values, ranging from big positive to 0 to big negative values. 我有一个包含三列的矩阵,值的变化很大,从大正值到0到大负值。 For the sake of better representation of data I want to take log2 of all value but as it is not possible to take log2 of negative values and 0, I want to do following:
为了更好地表示数据,我想取所有值的log2,但由于不可能取负值和0的log2,我想执行以下操作:
I am trying to do this with following code but no success so far: 我正在尝试使用以下代码来完成此操作,但到目前为止没有成功:
Log2Transformed <- ifelse(df == 0, 1, log2(df) & ifelse(df < 0, -log2(abs(df)), log2(df)))
head(df)
Open_TD Close_TD Invariant_TD
[1,] 1 6 5
[2,] 2 2 4
[3,] 0 0 -1
[4,] 0 0 2
[5,] NA 0 2
[6,] NA 0 1
Another way of doing this would be to make use of the $sign$ function, the 0 you would still need to replace in a seperate step eg 这样做的另一种方式是利用$ sign $函数,您仍然需要在单独的步骤中替换0,例如
test <- rnorm(100)
abs_log <- function(x){
x[x==0] <- 1
si <- sign(x)
si * log2(si*x)
}
boxplot(abs_log(test))
There are probably clever ways of doing this, but I would take my time and clearer define each step. 可能有一些聪明的方法可以做到这一点,但是我会花时间,并更清楚地定义每个步骤。
## Create dummy data
dd = data.frame(x = c(0, rnorm(100)))
First create a column for the transformed data 首先为转换后的数据创建一列
dd$trans = dd$x
Then gradually manipulate the column following your rules 然后按照您的规则逐步操作该列
#If number = 0 then change it to 1 and take log2
dd$trans[dd$x==0] = log2(1)
#If number < 0 then take log2 of absolute value
# and assign the negative number to it
dd$trans[dd$x< 0] = -log2(abs(dd$x[dd$x <0]))
#If number > 0 then take log2 of the number
dd$trans[dd$x> 0] = log2(dd$x[dd$x >0])
Before plotting 绘图前
boxplot(dd$trans)
I would create a function called trans_log2
that would automatically do this, eg 我将创建一个名为
trans_log2
的函数,该函数将自动执行此操作,例如
dd$x = trans_log2(dd$x)
let's do this constructively: 让我们建设性地做到这一点:
if x > 0
we log it. 如果
x > 0
我们将其记录。
if x == 0
we replace it with 1 then log. 如果
x == 0
我们将其替换为1,然后登录。
if x < 0
we negate, then log, then negate again. 如果
x < 0
我们求反,然后对数,然后再求反。 that is, if we have a negative, say x= -y, y>0
the output should be -1*log(y)
which is exactly the result of log(1/y)
. 也就是说,如果我们有一个负数,例如
x= -y, y>0
则输出应为-1*log(y)
,这正是log(1/y)
。
so we'd like to replace each negative x
with 1/abs(x)
while not hurting our positives. 因此我们希望将每个负数
x
替换为1/abs(x)
同时又不损害我们的正数。 clearly abs(x)
would not affect the positives, and the way of indicating the negatives is their sign, given by sign(x)
. 显然
abs(x)
不会影响正数,指示负数的方式是它们的符号,由sign(x)
。 exponentiation by sign would replace only the negatives with their reciprocals. 通过符号求幂只会将负数替换为其倒数。
all in all, our solution to the value substitution would be (abs(x))^(sign(x))
and then we can happily log2
, so we get: 总而言之,我们对值替换的解决方案将是
(abs(x))^(sign(x))
,然后我们可以很高兴地log2
,所以我们得到:
Log2Transformed <- log2((abs(df))^(sign(df)))
for this input (based on your example): 对于此输入(基于您的示例):
Open_TD Close_TD Invariant_TD
1 1.0 6 5
2 2.0 2 4
3 -32.0 0 -1
4 -0.5 0 2
5 NA 0 0
6 NA 0 1
we get the following output: 我们得到以下输出:
Open_TD Close_TD Invariant_TD
[1,] 0 2.584963 2.321928
[2,] 1 1.000000 2.000000
[3,] -5 0.000000 0.000000
[4,] 1 0.000000 1.000000
[5,] NA 0.000000 0.000000
[6,] NA 0.000000 0.000000
one-liner, no extra functions, no need to actually change the original data or create new dataframes and to top it all uses the matrix script which is typical to R and MatLab. 单行代码,无额外功能,无需实际更改原始数据或创建新的数据框,最重要的是,使用R和MatLab常用的矩阵脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.