简体   繁体   中英

Normalizing different Columns with Different Scales in R

I merged several datasets and now confronting with challenge of scaling.

Suppose that in a specific dataset, the scale of a column is 1-10, while from another dataset, the scale is 1-4.

How can I make these columns into the same scale (eg 1-10) in R?

Generally, if you have a vector x and you want to linearly transform x so its range is from r1 to r2 , you transform it like this:

result = (x - min(x)) / (max(x) - min(x)) * (r2 - r1) + r1

We could put this in a convenient function that handles NA values nicely:

rescale = function(x, range) {
  rx = range(x, na.rm = TRUE)
  (x - rx[1]) / diff(rx)  * diff(range) + range[1]
}

Which you could use like this:

rescale(1:4, range = c(1, 10))
# [1]  1  4  7 10

Or in your particular case, your_data$col1to4 = rescale(your_data$col1to4, range = range(your_data$col1to10))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM