Skip to content

Commit

Permalink
Updated function definitions with argument defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack-83 committed Jul 12, 2018
1 parent 68f0833 commit 99f113f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
8 changes: 4 additions & 4 deletions R/center.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 7 additions & 5 deletions R/rescale.R
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions man/center.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions man/rescale.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions tests/testthat/test-center.R
Original file line number Diff line number Diff line change
@@ -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))
})
6 changes: 5 additions & 1 deletion tests/testthat/test-rescale.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit 99f113f

Please sign in to comment.