Search code examples
rrangecenterscalingmedian

How can I rescale my variable so that its median is 0, its min does not exceed -1 and its max does not exceed +1?


I am trying to create boxplots where the medians of my variables are aligned at 0. Their range do not have to be fixed at [-1, 1], but I would like their min and max to fall within this range. Is there an operation that I could use?

I was able to normalize my variables in [-1, 1], but I am aiming at having the medians aligned at 0, and their range just falling within (not being fixed at) [-1, 1].


Solution

  • Here's a function to do that. It finds which extreme is farther from the median and then uses the median and the max distance from the median to scale everything into a range between -1 and 1, with median at the center. This will break if the data has no range (ie min = median = max), as that will result in an infinite rescaling factor, but I'm not sure what the expected behavior should be in that case.

    rescale_center_median <- function(my_numbers) {
      my_median = median(my_numbers, na.rm = TRUE)
      my_range = range(my_numbers, na.rm = TRUE)
      scale_factor = max(abs(my_range-my_median))
      (my_numbers - my_median) / scale_factor
    }
    

    Testing:

    set.seed(42)
    rescale_center_median(rnorm(10))
    # [1]  0.60393025 -0.58015650 -0.01258313  0.15241963  0.01258313 -0.29963620  0.68991628
    # [8] -0.29262249  1.00000000 -0.27308102
    median(scaled_numbers)
    #[1] 0
    > range(scaled_numbers)
    [1] -0.4922334  1.0000000