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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.