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

rescale Date (and POSIXct) objects? #74

Closed
zeehio opened this issue Mar 1, 2016 · 3 comments
Closed

rescale Date (and POSIXct) objects? #74

zeehio opened this issue Mar 1, 2016 · 3 comments
Labels

Comments

@zeehio
Copy link
Contributor

@zeehio zeehio commented Mar 1, 2016

Hi,

As of today it is not possible to rescale dates (and times) using scales::rescale. Is there a particular reason? I am puzzled by this, and I think that I must be missing something, because the problem seems fairly simple to tackle (using as.numeric somewhere). I would like to contribute with a clean solution, but I am afraid I am being naive on the solution. Any advice? (ggplot2::geom_segment could benefit from this)

days <- as.Date(c("2016-03-01", "2016-03-02", "2016-03-03"))
scales::rescale(days[2], from = c(days[1], days[3])) == 0.5

datetimes <- as.POSIXct(c("2016-03-01 08:05:00",
                          "2016-03-01 08:10:00",
                          "2016-03-01 08:15:00"),
                        format = "%Y-%m-%d %H:%M:%S")
testthat::expect_equal(scales::rescale(datetimes[2],
                       from = c(datetimes[1], datetimes[3])), 0.5)

I get

Error in Math.Date(x) : abs not defined for "Date" objects
@hadley
Copy link
Member

@hadley hadley commented Mar 1, 2016

I think coercing back and forth between numerics is probably the right thing to do.

@zeehio
Copy link
Contributor Author

@zeehio zeehio commented Nov 1, 2016

I just rebased the pull request on the latest master. Is there anything else I can do to get this merged?

Thank you for your time

@zeehio
Copy link
Contributor Author

@zeehio zeehio commented Jun 27, 2017

This is already merged in #75, so I will close it

@zeehio zeehio closed this Jun 27, 2017
zeehio added a commit to zeehio/scales that referenced this issue Oct 17, 2017
The idea behind r-lib#74 and r-lib#75 was to use an S3 method so we never coerced to numeric
unless we had to. The aim was to prevent future cases similar to the integer64 one
where the coercion to numeric implies a loss of precision.

Reality is that there are many more numeric types out there that do not inherit from the numeric
class (like the dist object mentioned in r-lib#102), compared to the number of cases similar to integer64, where the numeric coercion is wrong.

So there is a trade-off:

- Not providing the `default` methods has the advantage that we only rescale the classes that we know can be rescaled, and the disadvantage that we get issues like r-lib#102 when we find a new object. This is what was implemented in r-lib#75

- Providing `rescale.default` and `rescale_mid.default` has the advantage of catching all the numeric-like types that do not inherit the numeric class (such as the dist object) and working as expected. However, we risk to deliver wrong results without an error if a case similar to integer64 appeared. This is what this commit implements.
zeehio added a commit to zeehio/scales that referenced this issue Jun 9, 2018
The idea behind r-lib#74 and r-lib#75 was to use an S3 method so we never coerced to numeric
unless we had to. The aim was to prevent future cases similar to the integer64 one
where the coercion to numeric implies a loss of precision.

Reality is that there are many more numeric types out there that do not inherit from the numeric
class (like the dist object mentioned in r-lib#102), compared to the number of cases similar to integer64, where the numeric coercion is wrong.

So there is a trade-off:

- Not providing the `default` methods has the advantage that we only rescale the classes that we know can be rescaled, and the disadvantage that we get issues like r-lib#102 when we find a new object. This is what was implemented in r-lib#75

- Providing `rescale.default` and `rescale_mid.default` has the advantage of catching all the numeric-like types that do not inherit the numeric class (such as the dist object) and working as expected. However, we risk to deliver wrong results without an error if a case similar to integer64 appeared. This is what this commit implements.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants