Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: rescale transformation #415

Open
teunbrand opened this issue Dec 15, 2023 · 2 comments
Open

Feature request: rescale transformation #415

teunbrand opened this issue Dec 15, 2023 · 2 comments

Comments

@teunbrand
Copy link
Contributor

I'd like to propose a new transformation function that essentially wraps the rescale() function. It's definition can be something like the following:

library(scales)

transform_rescale <- function(to = c(0, 1), from = c(0, 1)) {
  force(to)
  force(from)
  new_transform(
    "rescaling",
    transform = function(x) rescale(x, to = to, from = from),
    inverse   = function(x) rescale(x, to = from, from = to) 
  )
}

It should be able to handle a bunch of 'scale and translate' transformations, like unit conversions. Going from metres to inches is simply a scale transformation.

metres_to_inches <- transform_rescale(to = c(0, 100/2.54))
metres_to_inches$transform(1)
#> [1] 39.37008

Whereas going from degrees celcius to degrees fahrenheit is a 'scale and translate' transformation.

celsius_to_fahrenheit <- transform_rescale(to = c(32, 212), from = c(0, 100))
celsius_to_fahrenheit$transform(-5)
#> [1] 23
celsius_to_fahrenheit$transform(32)
#> [1] 89.6

Created on 2023-12-15 with reprex v2.0.2

Given that it is suitable for plenty of unit conversions, I think this transformation may have a bunch of applications.

@mjskay
Copy link
Contributor

mjskay commented Dec 16, 2023

In case it is desired, adding derivative support for this should entail something like:

transform_rescale <- function(to = c(0, 1), from = c(0, 1)) {
  force(to)
  force(from)
  new_transform(
    "rescaling",
    transform = function(x) rescale(x, to = to, from = from),
    inverse   = function(x) rescale(x, to = from, from = to),
    d_transform = function(x) rep(diff(to) / diff(from), length(x)),
    d_inverse = function(x) rep(diff(from) / diff(to), length(x))
  )
}

@teunbrand
Copy link
Contributor Author

Ah yes, of course! Thanks for weighing in, it was a little oversight on my part

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants