Search code examples
rdataframerownamesdivide

Divide rows in a given column by a name-speficied row


Having a dataframe in R, I'd like to divide all rows in a given column by a name-speficied row (here 'ABC') from the same column, and apply these to all the columns using this specific row as the normalizer.

Input:

A   1   1
B   1   2
C   4   4
ABC 2   2
E   2   3

Output:

A   0.5 0.5
B   0.5 1
C   2   2
ABC 1   1
E   1   1.5

Many thanks in advance!


Solution

  • Another option is

    df1[-1] <- df1[-1]/df1[df1$v1=='ABC', -1][col(df1[-1])]