Search code examples
rdataframedata-conversion

Convert values ranging from (0 to +1) to negative values in R


I have question regarding the conversion of values in the data frame in R. I have the calculated data that consists of values ranging from (0 to 0.999) and (1 to infinity). I would like to retain the (1 to infinity) values as it is in the table, however, would like to convert (0 to 0.999) to negative values for the purpose of plotting the heatmap using the formula (-1/[(0 to 0.999)]) . Below is the example of my input and output data. Please assist me with this.

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"))

Thank you, Toufiq


Solution

  • We can change only the values that are in range of 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