Added base Matlab function
This commit is contained in:
parent
e5cf85d9d4
commit
4c4390f419
3 changed files with 45 additions and 0 deletions
13
R/isempty.R
Normal file
13
R/isempty.R
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#' @title Is Array Empty?
|
||||
#' @description Determine whether array is empty. An empty array, table, or timetable has at least one dimension with length 0, such as 0-by-0 or 0-by-5.
|
||||
#' @details Emulates the behavior of the `isempty` function on Matlab
|
||||
#' @param x array
|
||||
#'
|
||||
isempty <- function(x) {
|
||||
if (class(x) %in% c("array", "matrix")) {
|
||||
dim_mat_x <- dim(x)
|
||||
} else {
|
||||
dim_mat_x <- dim(matrix(x))
|
||||
}
|
||||
return(any(dim_mat_x == 0) | is.null(dim_mat_x))
|
||||
}
|
||||
17
man/isempty.Rd
Normal file
17
man/isempty.Rd
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/isempty.R
|
||||
\name{isempty}
|
||||
\alias{isempty}
|
||||
\title{Is Array Empty?}
|
||||
\usage{
|
||||
isempty(x)
|
||||
}
|
||||
\arguments{
|
||||
\item{x}{array}
|
||||
}
|
||||
\description{
|
||||
Determine whether array is empty. An empty array, table, or timetable has at least one dimension with length 0, such as 0-by-0 or 0-by-5.
|
||||
}
|
||||
\details{
|
||||
Emulates the behavior of the `isempty` function on Matlab
|
||||
}
|
||||
|
|
@ -129,3 +129,18 @@ test_that("strcmp works as expected", {
|
|||
expect_error(strcmp(s2, s3))
|
||||
expect_equal(strcmp(s4, s5), matrix(c(FALSE, TRUE, FALSE, TRUE), 2))
|
||||
})
|
||||
|
||||
test_that("isempty works as expected", {
|
||||
A <- array(dim=c(0, 2, 2))
|
||||
B <- matrix(rep(NA, 4), 2)
|
||||
C <- matrix(rep(0, 4), 2)
|
||||
cat1 <- as.factor(c(NA, NA))
|
||||
cat2 <- as.factor(c())
|
||||
str1 <- matrix(rep("", 3))
|
||||
expect_true(isempty(A))
|
||||
expect_false(isempty(B))
|
||||
expect_false(isempty(C))
|
||||
expect_false(isempty(cat1))
|
||||
expect_true(isempty(cat2))
|
||||
expect_false(isempty(str1))
|
||||
})
|
||||
Loading…
Add table
Reference in a new issue