Added basic translation of "find"

This commit is contained in:
Waldir Leoncio 2020-03-18 10:30:37 +01:00
parent ac05f90b87
commit 42ada1d34a
3 changed files with 28 additions and 0 deletions

9
R/find.R Normal file
View file

@ -0,0 +1,9 @@
#' @title Find indices and values of nonzero elements
#' @description Emulates behavior of `find`
find <- function(x) {
if (is.logical(x)) {
return(which(x))
} else {
return(which(x > 0))
}
}

11
man/find.Rd Normal file
View file

@ -0,0 +1,11 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/find.R
\name{find}
\alias{find}
\title{Find indices and values of nonzero elements}
\usage{
find(x)
}
\description{
Emulates behavior of `find`
}

View file

@ -143,4 +143,12 @@ test_that("isempty works as expected", {
expect_false(isempty(cat1))
expect_true(isempty(cat2))
expect_false(isempty(str1))
})
test_that("find works as expected", {
X <- matrix(c(1, 0, 2, 0, 1, 1, 0, 0, 4), 3, byrow=TRUE)
Y <- seq(1, 19, 2)
expect_equal(find(X), c(1, 5, 7, 8, 9))
expect_equal(find(!X), c(2, 3, 4, 6))
expect_equal(find(Y == 13), 7)
})