Home > Software engineering >  Calculating percentage for each value in multiply columns. based on maximum value in each column usi
Calculating percentage for each value in multiply columns. based on maximum value in each column usi

Time:11-14

I am trying to form a loop where it takes the maximum value from each column, then use it to calculate the percentage for the other values within each column. Someone asked a similar question in the past before, however the solution given was calculating the percentages the opposite way round to what I need.

My df:

a b c d
1115.68 67.52 151.13 183.32
1083.16 53.75 136.70 202.56
1076.69 56.07 157.77 196.43

Desire output:

a b c d
100.00 100.00 95.79 90.50
97.09 79.61 86.65 100.00
96.51 83.05 100.00 96.97

Current output, I am getting the percentages in the opposite way round:

a b c d
100.00 6.05 13.55 16.43
100.00 4.96 12.62 18.70
100.00 5.21 14.65 18.24

The code I was using is this:

output <- df
for (i in 1:nrow(df)) {
  highvalue <- max(df[i, 1:4])
  for (j in 1:4) {
    output[i, j] <- df[i, j] * 100/highvalue
  }
}
output

What is wrong with the code?

CodePudding user response:

You find maximum in row, not in column. Loop version:

output <- df
for (i in 1:ncol(df)) {
  highvalue <- max(df[, i])
  output[, i] <- output[, i] / highvalue * 100
}

But You can use good version of code:

library(dplyr)
output <- df / df %>% lapply(max) * 100
  • Related