From 9acf673f4b5072c6defc39ac0ba334c16df32bbf Mon Sep 17 00:00:00 2001 From: Martin Jung Date: Thu, 5 Dec 2024 11:22:44 +0100 Subject: [PATCH] :rocket: negated in function --- NAMESPACE | 1 + R/misc_notIn.R | 19 +++++++++++++++++++ man/grapes-notin-grapes.Rd | 27 +++++++++++++++++++++++++++ tests/testthat/test-misc_tests.R | 8 +++++++- 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 R/misc_notIn.R create mode 100644 man/grapes-notin-grapes.Rd diff --git a/NAMESPACE b/NAMESPACE index 29a6e11..e8b90d0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export("%notin%") export(misc_sanitizeNames) export(sp_replaceGriddedNA) export(sp_resampleRas) diff --git a/R/misc_notIn.R b/R/misc_notIn.R new file mode 100644 index 0000000..8b6bf6e --- /dev/null +++ b/R/misc_notIn.R @@ -0,0 +1,19 @@ +#' Inverse of 'in' call +#' +#' @description +#' Calculates the set of entries not present in the second vector. +#' Added for convenience since this is not supported by default in R. +#' +#' @param a First [`vector`] object. +#' @param b Second [`vector`] object. +#' +#' @returns A [`vector`] of [`logical`] entries of \code{'a'} not present in \code{'b'}. +#' @examples +#' # example code +# lu <- c("Forest", "Cropland", "Wetland", "OtherLand") +# lu2 <- c("Forest", "Wetland", "otherland") +# which(lu %notin% lu2) +#' @keywords utils +#' @author Martin Jung +#' @export +`%notin%` = function(a, b){!(a %in% b)} diff --git a/man/grapes-notin-grapes.Rd b/man/grapes-notin-grapes.Rd new file mode 100644 index 0000000..3956e75 --- /dev/null +++ b/man/grapes-notin-grapes.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/misc_notIn.R +\name{\%notin\%} +\alias{\%notin\%} +\title{Inverse of 'in' call} +\usage{ +a \%notin\% b +} +\arguments{ +\item{a}{First \code{\link{vector}} object.} + +\item{b}{Second \code{\link{vector}} object.} +} +\value{ +A \code{\link{vector}} of \code{\link{logical}} entries of \code{'a'} not present in \code{'b'}. +} +\description{ +Calculates the set of entries not present in the second vector. +Added for convenience since this is not supported by default in R. +} +\examples{ +# example code +} +\author{ +Martin Jung +} +\keyword{utils} diff --git a/tests/testthat/test-misc_tests.R b/tests/testthat/test-misc_tests.R index d2181bf..2350eb9 100644 --- a/tests/testthat/test-misc_tests.R +++ b/tests/testthat/test-misc_tests.R @@ -1,3 +1,9 @@ -test_that("Basic unit tests for misc functions", { +test_that("Basic unit tests for small misc functions", { expect_equal(misc_sanitizeNames("Climate-temperature2015"), "Climate_temperature2015") + + # Not in function\ + lu <- c("Forest", "Cropland", "Wetland", "OtherLand") + lu2 <- c("Forest", "Wetland", "otherland") + test <- which(lu %notin% lu2) + expect_equal(test, c(2,4)) })