Added basic colon function

This commit is contained in:
Waldir Leoncio 2020-01-15 15:18:49 +01:00
parent f8f65f176c
commit 1b79529e4b
3 changed files with 33 additions and 0 deletions

12
R/colon.R Normal file
View file

@ -0,0 +1,12 @@
#' @title Vector creation
#' @description Simulates the function `colon()` and its equivalent `:` operator from Matlab, which have a similar but not quite equivalent behavior when compared to `seq()` and `:` in R.
#' @param a initial number
#' @param b final number
#' @export
colon <- function(a, b) {
if (a <= b) {
return(a:b)
} else {
return(vector(mode = "numeric"))
}
}

16
man/colon.Rd Normal file
View file

@ -0,0 +1,16 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/colon.R
\name{colon}
\alias{colon}
\title{Vector creation}
\usage{
colon(a, b)
}
\arguments{
\item{a}{initial number}
\item{b}{final number}
}
\description{
Simulates the function `colon()` and its equivalent `:` operator from Matlab, which have a similar but not quite equivalent behavior when compared to `seq()` and `:` in R.
}

View file

@ -60,4 +60,9 @@ test_that("times works as expected", {
object = times(matrix(c(-1.6, 5), 1), c(8, 1)),
expected = matrix(c(-12.8, -1.6, 40, 5), 2)
)
})
test_that("colon works as expected (hee hee)", {
expect_equal(colon(1, 4), 1:4)
expect_length(colon(4, 1), 0)
})