【问题标题】:Convert values ranging from (0 to +1) to negative values in R将范围从(0到+1)的值转换为R中的负值
【发布时间】:2020-05-06 01:49:53
【问题描述】:

我对 R 中数据框中的值转换有疑问。我的计算数据由 (0 到 0.999) 和 (1 到无穷大) 范围内的值组成。我想保留表中的(1 到无穷大)值,但是,为了使用公式 (-1/[(0 to 0.999)]) 绘制热图,我想将(0 到 0.999)转换为负值。下面是我的输入和输出数据的示例。请帮我解决这个问题。

Input_Data <- read.csv(file = "./Synthetic_Data_Input.csv",stringsAsFactors = FALSE, row.names = 1)
Output_Data <- read.csv(file = "./Synthetic_Data_Output.csv",stringsAsFactors = FALSE, row.names = 1)

dput(Input_Data)
structure(list(Sample_1 = c(0.942972, 1.00812, 1.10038, 1.19572, 
1.28194, 1.37384, 1.75378, 2.56584, 2.56584), Sample_3 = c(0.517607, 
0.935699, 0.567427, 0.572821, 0.795282, 0.541105, 1.03596, 2.78013, 
0.189337)), class = "data.frame", row.names = c("Gene_4", "Gene_7", 
"Gene_2", "Gene_3", "Gene_6", "Gene_9", "Gene_5", "Gene_8", "Gene_1"
))


dput(Output_Data)
structure(list(Sample_1 = c(-1.060476875, 1.00812, 1.10038, 1.19572, 
1.28194, 1.37384, 1.75378, 2.56584, 2.56584), Sample_3 = c(-1.93196769, 
-1.068719749, -1.762341235, -1.745746053, -1.257415609, -1.848070153, 
1.03596, 2.78013, -5.281587857)), class = "data.frame", row.names = c("Gene_4", 
"Gene_7", "Gene_2", "Gene_3", "Gene_6", "Gene_9", "Gene_5", "Gene_8", 
"Gene_1"))

谢谢, 图菲克

【问题讨论】:

    标签: r dataframe data-conversion


    【解决方案1】:

    我们只能更改0 to 0.999 范围内的值。

    inds <- Input_Data <= 0.999
    Input_Data[inds] <- -1/Input_Data[inds]
    
    Input_Data
    #        Sample_1  Sample_3
    #Gene_4 -1.060477 -1.931968
    #Gene_7  1.008120 -1.068720
    #Gene_2  1.100380 -1.762341
    #Gene_3  1.195720 -1.745746
    #Gene_6  1.281940 -1.257416
    #Gene_9  1.373840 -1.848070
    #Gene_5  1.753780  1.035960
    #Gene_8  2.565840  2.780130
    #Gene_1  2.565840 -5.281588
    

    【讨论】:

    • 优雅的解决方案。
    • 嗨@Ronak Shah,我收到如下错误,这是因为我的数据中缺少值:[&lt;-.data.frame(*tmp*, inds, value = c(- 4484.10385184521, : 'value' 长度错误
    • @MohammedToufiq 将 inds 更改为 inds &lt;- Input_Data &lt;= 0.999 &amp; !is.na(Input_Data) 以删除 NA 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多