diff --git a/R/center.R b/R/center.R index 3542987..3d32e59 100644 --- a/R/center.R +++ b/R/center.R @@ -5,13 +5,13 @@ #' And even more #' #' @param dataset The numeric vector to be centered -#' @param desired The numeric midpoint value around which the data will be centered +#' @param desired The numeric midpoint value around which the data will be centered (default: 0) #' #' @return A new vector containing the original data centered around the desired values #' #' @examples -# center(c(1,2,3),0) # should return -1 0 1 +# center(c(1,2,3)) # should return -1 0 1 # center(c(4,5,6),2) # should return 1 2 3 -center <- function(dataset, desired) { - dataset - mean(dataset) + desired +center <- function(dataset, desired = 0) { + dataset - mean(dataset) + desired } diff --git a/R/rescale.R b/R/rescale.R index 1ee7405..c90e6d2 100644 --- a/R/rescale.R +++ b/R/rescale.R @@ -1,12 +1,14 @@ #' Rescaling datasets #' #' @param v A numeric vector +#' @param lower numeric (default:0) +#' @param upper numeric (default:1) #' -#' @return A new numeric vector, rescaled from the input vector to the range of 0 to 1 +#' @return A new numeric vector, rescaled from the input vector to a range defined by the `lower` and `upper` parameters (default: 0 and 1) #' #' @examples -#' rescale(c(1,2,3)) # [1] 0.0 0.5 1.0 -#' rescale(c(1,2,3,4,5)) # [1] 0.00 0.25 0.50 0.75 1.00 -rescale <- function(v) { - (v - min(v)) / (max(v) - min(v)) +#' rescale2(c(1,2,3)) # [1] 0.0 0.5 1.0 +#' rescale2(c(1,2,3,4,5),1,2) # [1] 1.00 1.25 1.50 1.75 2.00 +rescale <- function(v, lower = 0, upper = 1) { + (v - min(v)) / (max(v) - min(v)) * (upper - lower) + lower } diff --git a/man/center.Rd b/man/center.Rd index cda30de..d17da35 100644 --- a/man/center.Rd +++ b/man/center.Rd @@ -4,12 +4,12 @@ \alias{center} \title{Centering datasets} \usage{ -center(dataset, desired) +center(dataset, desired = 0) } \arguments{ \item{dataset}{The numeric vector to be centered} -\item{desired}{The numeric midpoint value around which the data will be centered} +\item{desired}{The numeric midpoint value around which the data will be centered (default: 0)} } \value{ A new vector containing the original data centered around the desired values diff --git a/man/rescale.Rd b/man/rescale.Rd index 38a1db5..3fcf405 100644 --- a/man/rescale.Rd +++ b/man/rescale.Rd @@ -4,18 +4,22 @@ \alias{rescale} \title{Rescaling datasets} \usage{ -rescale(v) +rescale(v, lower = 0, upper = 1) } \arguments{ \item{v}{A numeric vector} + +\item{lower}{numeric (default:0)} + +\item{upper}{numeric (default:1)} } \value{ -A new numeric vector, rescaled from the input vector to the range of 0 to 1 +A new numeric vector, rescaled from the input vector to a range defined by the `lower` and `upper` parameters (default: 0 and 1) } \description{ Rescaling datasets } \examples{ -rescale(c(1,2,3)) # [1] 0.0 0.5 1.0 -rescale(c(1,2,3,4,5)) # [1] 0.00 0.25 0.50 0.75 1.00 +rescale2(c(1,2,3)) # [1] 0.0 0.5 1.0 +rescale2(c(1,2,3,4,5),1,2) # [1] 1.00 1.25 1.50 1.75 2.00 } diff --git a/tests/testthat/test-center.R b/tests/testthat/test-center.R index d7d94fd..2e344c3 100644 --- a/tests/testthat/test-center.R +++ b/tests/testthat/test-center.R @@ -1,5 +1,6 @@ context("test-center.R") -test_that("centering works", { - expect_equal(center(c(1,2,3),0), c(-1,0,1)) +test_that("centering works, also relying on default of desired parameter", { + expect_equal(center(c(1,2,3)), c(-1,0,1)) + expect_equal(center(c(1,2,3),1), c(0,1,2)) }) diff --git a/tests/testthat/test-rescale.R b/tests/testthat/test-rescale.R index 6310a4d..481ba24 100644 --- a/tests/testthat/test-rescale.R +++ b/tests/testthat/test-rescale.R @@ -1,5 +1,9 @@ context("test-rescale.R") -test_that("rescaling works", { +test_that("rescaling works, without overwriting the default", { expect_equal(rescale(1:5), 0.25*(0:4)) }) + +test_that("rescaling works, without nondefault arguments", { + expect_equal(rescale(1:3,1,2), c(1,1.5,2)) +})