Question: Are there any functions to subtract all features(rows) to a particular value(row) in the same data file?
1
11 months ago by
mohammedtoufiq91110 wrote:

I am new to programming in R and Python, however have some basics. I have a technical question about computation. I would like to know if there are any functions for performing subtraction of all features(rows) to a particular value (row) from the same data list. I would like to obtain the output_value1 as shown below and post this, multiply by (-1) to obtain the output_value2.

Please let me know if you need more details.

I have tried performing the same operation in the MS Excel, this is very tedious and time consuming.

I have many large datasets with several hundred rows and columns which becomes more complex to manually perform the same in MS Excel. Hence, I would prefer to write a code and obtain the desired outputs.

``````    |Feature| |Value| |Output_value1| |Output_value2|

|Gene_1| |14.25633934| |0.80100922| |-0.80100922|

|Gene_2| |16.88394578| |3.42861566| |-3.42861566|

|Gene_3| |16.01| |2.55466988| |-2.55466988|

|Gene_4| |13.82329514| |0.36796502| |-0.36796502|

|Gene_5| |12.96382949| |-0.49150063| |0.49150063|

|Normalizer| |13.45533012| |0| |0|
``````
matrix normalization python R • 295 views
modified 11 months ago by zx87549.4k • written 11 months ago by mohammedtoufiq91110
3
11 months ago by
zx87549.4k
London
zx87549.4k wrote:

Try this example:

``````# example data
df1 <- read.table(text = "
Feature Value
Gene_1  14.25633934
Gene_2  16.88394578
Gene_3  16.01
Gene_4  13.82329514
Gene_5  12.96382949
Normalizer  13.45533012
", header = TRUE)

# find the row with normaliser, use the value to substract from each row
df1\$Output_value1 <- df1\$Value - df1[ df1\$Feature == "Normalizer", "Value"]

# then flip the sign (could be skipped, if we flip the substract in above step)
df1\$Output_value2 <- df1\$Output_value1 * -1
``````

Thank you Tokhir. This was helpful and worked fine.

But if I have hundreds of values/sample columns in the data file, for instance as given below:

`````` |Feature| |Value| |Value2| |Value3| |Value4| |Value5| |Value...n|
``````

How could this be modified? For this purpose, can I change the function to df1\$Output_value1 <- df1\$Value[1:n] - df1[ df1\$Feature == "Normalizer", "Value"]

where n is the number of values/samples.

1

Provide representative example data.

example data

example data attached in the link.

Where,

O.Value1 = Value1 of [gene_1, gene_2, gene_3, gene_4, gene_5] - [Value1 of Normalizer]

Negative.O.Value1 = (-1) *O.Value1

till

O.Value13

Negative.O.Value13

ADD REPLYlink modified 11 months ago • written 11 months ago by mohammedtoufiq91110
1

We can use sweep:

``````cbind(
df1,
sweep(df1[, -1], 2, unlist(df1[ df1\$Feature == "Normalizer", -1 ]), "-"),
-sweep(df1[, -1], 2, unlist(df1[ df1\$Feature == "Normalizer", -1 ]), "-")
)
``````

Thank you. Just one more question,

In case, I would like to print only specific columns as shown in the attached example data file from the O.Value1 to O.Value13 columns without the populating other columns, how can I deal with it? And later, print separately (-1) *O.Values as a different list. Does the below code looks fine?

To obtain output values

O.Value <- cbind( sweep(df1[, -1], 2, unlist(df1[ df1\$Feature == "Normalizer", -1 ]), "-") )

To obtain Negative output values

Negative.O.Value <- cbind( -sweep(df1[, -1], 2, unlist(df1[ df1\$Feature == "Normalizer", -1 ]), "-") )

ADD REPLYlink modified 11 months ago • written 11 months ago by mohammedtoufiq91110
1
11 months ago by
EMBL Heidelberg, Germany
Jean-Karim Heriche23k wrote:

This is basic R programming or I don't understand the question.
In R, assuming the data shown is in data frame df:

``````df\$Output_value2 <- df\$Value[6] - df\$Value
``````