Search code examples
rrownormalizationnormalize

Normalisation of data in rows in R


I'd like to normalize values in rows of a data frame. In other words, divide every row in a given column by a ratio calculated from the sum of all the rows in a given column to the lowest sum of the rows. Here is my example:

df <- data.frame(x = 5:1, y = 2:6, z = 11:25)

finding a column with the lowest sum

sum(df$x)
sum(df$y)
sum(df$z)

getting normalised values

df$x_norm <- df$x/(sum(df$x)/sum(df$x))
df$y_norm <- df$y/(sum(df$y)/sum(df$x))
df$z_norm <- df$z/(sum(df$z)/sum(df$x))

Solution

  • Instead of repeating the operation for each column separately, we can do this in a faster way by getting the column sums (colSums), divide it by the minimum of 'v1', and finally divide the dataset by 'v2' to create the new columns

    v1 <- colSums(df)
    v2 <- v1/min(v1)
    df[paste0(names(df), "_norm")] <- t(t(df)/v2)