Added translated function + unit tests
This commit is contained in:
parent
2f6ad882a9
commit
4810df7fb9
3 changed files with 64 additions and 0 deletions
28
R/strcmp.R
Normal file
28
R/strcmp.R
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#' @title Compare two character elements
|
||||
#' @description Logical test if two character elements are identical
|
||||
#' @param s1 first character element (string, vector or matrix)
|
||||
#' @param s2 second character element (string, vector or matrix)
|
||||
#' @return a logical element of the same type as the input
|
||||
#' @export
|
||||
strcmp <- function(s1, s2) {
|
||||
if (length(s1) == 1 & length(s2) == 1) {
|
||||
# Both are scalars, comparison is straightforward
|
||||
return(identical(s1, s2))
|
||||
} else if (length(s1) == 1 & length(s2) > 1) {
|
||||
# s1 is a scalar and s2 is a vector or a matrix
|
||||
checks <- sapply(s2, function(s) s1 %in% s)
|
||||
if (is(s2, "matrix")) checks <- matrix(checks, nrow(s2))
|
||||
} else if (length(s1) > 1 & length(s2) == 1) {
|
||||
# s1 is a vector/matrix, s2 is a scalar
|
||||
checks <- sapply(s1, function(s) s2 %in% s)
|
||||
if (is(s1, "matrix")) checks <- matrix(checks, nrow(s1))
|
||||
} else {
|
||||
# s1 and s2 are vectors/matrices
|
||||
if (identical(dim(s1), dim(s2))) {
|
||||
checks <- as.matrix(s4 == s5)
|
||||
} else {
|
||||
stop("Inputs must be the same size or either one can be a scalar.")
|
||||
}
|
||||
}
|
||||
return(checks)
|
||||
}
|
||||
19
man/strcmp.Rd
Normal file
19
man/strcmp.Rd
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/strcmp.R
|
||||
\name{strcmp}
|
||||
\alias{strcmp}
|
||||
\title{Compare two character elements}
|
||||
\usage{
|
||||
strcmp(s1, s2)
|
||||
}
|
||||
\arguments{
|
||||
\item{s1}{first character element (string, vector or matrix)}
|
||||
|
||||
\item{s2}{second character element (string, vector or matrix)}
|
||||
}
|
||||
\value{
|
||||
a logical element of the same type as the input
|
||||
}
|
||||
\description{
|
||||
Logical test if two character elements are identical
|
||||
}
|
||||
|
|
@ -112,3 +112,20 @@ test_that("isfield works as on Matlab", {
|
|||
expected = c(TRUE, TRUE, FALSE, TRUE, FALSE)
|
||||
)
|
||||
})
|
||||
|
||||
test_that("strcmp works as expected", {
|
||||
yes <- 'Yes'
|
||||
no <- 'No'
|
||||
ja <- 'Yes'
|
||||
expect_false(strcmp(yes, no))
|
||||
expect_true(strcmp(yes, ja))
|
||||
s1 <- 'upon'
|
||||
s2 <- matrix(c('Once', 'upon', 'a', 'time'), 2, byrow=TRUE)
|
||||
s3 <- c('Once', 'upon', 'a', 'time')
|
||||
s4 <- matrix(c("A", "bc", "def", "G"), 2, byrow=TRUE)
|
||||
s5 <- matrix(c("B", "c", "def", "G"), 2, byrow=TRUE)
|
||||
expect_equal(strcmp(s1, s2), matrix(c(FALSE, FALSE, TRUE, FALSE), 2))
|
||||
expect_equivalent(strcmp(s1, s3), c(FALSE, TRUE, FALSE, FALSE))
|
||||
expect_error(strcmp(s2, s3))
|
||||
expect_equal(strcmp(s4, s5), matrix(c(FALSE, TRUE, FALSE, TRUE), 2))
|
||||
})
|
||||
Loading…
Add table
Reference in a new issue