Adam Amin
Adam Amin

Reputation: 1456

How to normalise values of specific columns

I have the following data:

  config class       Da1        Da2         Da3        Da4
      f1   sdf 351.54189  6.0096407 1068.465766 1323.02938
      f2   sdf 258.46798 58.7775532  136.351827   43.33182
      f3   sdf  46.18123  0.1496663    2.573316    0.00000

I would like to normalise the values of columns Da1 to Da4 based on the range of its row. More specfically, the ranges to be normalised are based on each config, for example, the values 351.54189 6.0096407 1068.465766 1323.02938 are the range for f1. In this case, the maximum value to be considered in the calculation is 1323.02938. To do that, I have written the following script:

ttemp <- function(){
  df <- read.csv("/Untitled 3.csv")
  df[,3:6] <- apply(df[,3:6],2,norm)
}

norm <- function(x, maxVal){
  min = 0
  y <- (x-min)/(maxVal-min)
  return(y)
}

My question is how can I specify the maximum value in each row? and is the way I applied the correct one to normalise the columns values?

Upvotes: 1

Views: 29

Answers (1)

akrun
akrun

Reputation: 887008

In the function, we can get the max value and pass it

ttemp <- function(){
  df <- read.csv("/Untitled 3.csv")
  # changed the MARGIN to 1 as it was not clear
  df[,3:6] <- apply(df[, 3:6], 1, function(x) norm(x, max(x, na.rm = TRUE)))
  df
  }

Or another option is scale

df[, 3:6] <- scale(df[,3:6])

In the OP's function, min is taken as 0

apply(df[, 3:6], 2, function(x) norm(x, max(x, na.rm = TRUE)))
#           Da1         Da2         Da3        Da4
#[1,] 1.0000000 0.102243805 1.000000000 1.00000000
#[2,] 0.7352409 1.000000000 0.127614596 0.03275197
#[3,] 0.1313676 0.002546317 0.002408422 0.00000000

If this is by row, then change the MARGIN to 1

t(apply(df[, 3:6], 1, function(x) norm(x, max(x, na.rm = TRUE))))

Upvotes: 1

Related Questions