Improvements to setdiff

This commit is contained in:
Waldir Leoncio 2020-10-19 15:57:15 +02:00
parent eba8705d9e
commit 6b1a1910e3
3 changed files with 26 additions and 4 deletions

View file

@ -34,6 +34,7 @@ export(randdir)
export(repmat)
export(rivinSisaltamienMjonojenLkm)
export(selvitaDigitFormat)
export(setdiff_MATLAB)
export(simulateAllFreqs)
export(simulateIndividuals)
export(simuloiAlleeli)

View file

@ -6,8 +6,12 @@
#' @return
#' @author Waldir Leoncio
#' @export
setdiff <- function(A, B, legacy = FALSE) {
values <- sort(unique(A[is.na(match(A, B))]))
# browser() # TEMP
setdiff_MATLAB <- function(A, B, legacy = FALSE) {
if (is(A, "numeric") & is(B, "numeric")) {
values <- sort(unique(A[is.na(match(A, B))]))
} else if (is(A, "data.frame") & is(B, "data.frame")) {
stop("Not implemented for data frames")
}
# TODO: add support for indices (if necessary)
return(values)
}

View file

@ -222,6 +222,23 @@ test_that("nargin works correctly", {
test_that("setdiff works as expected", {
A <- c(3, 6, 2, 1, 5, 1, 1)
B <- c(2, 4, 6)
expect_equal(setdiff(A, B), list(c(1, 3, 5), 4, 1, 5))
C <- c(1, 3, 5)
expect_equal(setdiff(A, B), C)
A <- data.frame(
Var1 = 1:5,
Var2 = LETTERS[1:5],
Var3 = c(FALSE, TRUE, FALSE, TRUE, FALSE)
)
B <- data.frame(
Var1 = seq(1, 9, by = 2),
Var2 = LETTERS[seq(1, 9, by = 2)],
Var3 = rep(FALSE, 5)
)
C <- data.frame(
Var1 = c(2, 4),
Var2 = c('B', 'D'),
Var3 = c(TRUE, TRUE)
)
expect_equal(setdiff(A, B), C)
# TODO: add more examples from https://se.mathworks.com/help/matlab/ref/double.setdiff.html;jsessionid=0d8d42582d4d299b8224403899f1
})