From 42ada1d34ad5f9d0015cd6438781756e2f921987 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Wed, 18 Mar 2020 10:30:37 +0100 Subject: [PATCH] Added basic translation of "find" --- R/find.R | 9 +++++++++ man/find.Rd | 11 +++++++++++ tests/testthat/test-convertedBaseFunctions.R | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 R/find.R create mode 100644 man/find.Rd diff --git a/R/find.R b/R/find.R new file mode 100644 index 0000000..be417dd --- /dev/null +++ b/R/find.R @@ -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)) + } +} \ No newline at end of file diff --git a/man/find.Rd b/man/find.Rd new file mode 100644 index 0000000..be4de89 --- /dev/null +++ b/man/find.Rd @@ -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` +} diff --git a/tests/testthat/test-convertedBaseFunctions.R b/tests/testthat/test-convertedBaseFunctions.R index 357ce70..94bb5a5 100644 --- a/tests/testthat/test-convertedBaseFunctions.R +++ b/tests/testthat/test-convertedBaseFunctions.R @@ -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) }) \ No newline at end of file