0

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 (e.g. 1-10) in R?

David
  • 1
  • 2
  • 2
    Does this answer your question? [Scale a series between two points](https://stackoverflow.com/questions/5468280/scale-a-series-between-two-points) – guasi Jul 13 '22 at 15:22

1 Answers1

1

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))

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294