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() works with Date, POSIXct and POSIXlt objects. #75
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
# If needed, converts input from classes that lack division and multiplication | ||
# to classes that can be scaled. | ||
# @param x vector of values to convert, if needed | ||
adapt_input_class <- function(x) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I follow the reasoning here. Why not just unconditionally coerce to numeric? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the coercion to numeric is done, there will be a warning when using some integer64 objects:
I wanted to provide an example were the
Even if we have tolerance issues in this case, I prefer to work towards making But you have the last word, though 😃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case I'd prefer to use an S3 generic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using an S3 generic is the best approach, my apologies for not thinking about them when I wrote this patch. I was now able to provide a method (and test case) for |
||
if (inherits(x, c("Date", "POSIXct", "POSIXlt", "POSIXt"))) { | ||
as.numeric(x) | ||
} else { | ||
x | ||
} | ||
} | ||
|
||
#' Rescale numeric vector to have specified minimum and maximum. | ||
#' | ||
#' @param x numeric vector of values to manipulate. | ||
|
@@ -11,6 +22,8 @@ | |
#' rescale(runif(50)) | ||
#' rescale(1) | ||
rescale <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE, finite = TRUE)) { | ||
x <- adapt_input_class(x) | ||
from <- adapt_input_class(from) | ||
if (zero_range(from) || zero_range(to)) { | ||
return(ifelse(is.na(x), NA, mean(to))) | ||
} | ||
|
@@ -31,6 +44,9 @@ rescale <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE, finite = TRUE | |
#' rescale_mid(runif(50), mid = 0.5) | ||
#' rescale_mid(1) | ||
rescale_mid <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE), mid = 0) { | ||
x <- adapt_input_class(x) | ||
from <- adapt_input_class(from) | ||
mid <- adapt_input_class(mid) | ||
if (zero_range(from) || zero_range(to)) return(rep(mean(to), length(x))) | ||
|
||
extent <- 2 * max(abs(from - mid)) | ||
|
@@ -49,6 +65,8 @@ rescale_mid <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE), mid = 0) | |
#' rescale_max(runif(50)) | ||
#' rescale_max(1) | ||
rescale_max <- function(x, to = c(0, 1), from = range(x, na.rm = TRUE)) { | ||
x <- adapt_input_class(x) | ||
from <- adapt_input_class(from) | ||
x / from[2] * to[2] | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be documented
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left the comments as conventional R comments, so it does not appear under man/. I can remove the comments completely if you prefer.