Implemented dataframe parsing on setdiff

This commit is contained in:
Waldir Leoncio 2021-01-15 08:59:48 +01:00
parent 34cebe8604
commit d47b84fb55
2 changed files with 11 additions and 1 deletions

View file

@ -9,7 +9,16 @@ setdiff_MATLAB <- function(A, B, legacy = FALSE) {
if (is(A, "numeric") & is(B, "numeric")) { if (is(A, "numeric") & is(B, "numeric")) {
values <- sort(unique(A[is.na(match(A, B))])) values <- sort(unique(A[is.na(match(A, B))]))
} else if (is(A, "data.frame") & is(B, "data.frame")) { } else if (is(A, "data.frame") & is(B, "data.frame")) {
stop("Not implemented for data frames") C <- A
exclude_rows <- vector()
for (r1 in seq_len(nrow(A))) {
for (r2 in seq_len(nrow(B))) {
if (all(A[r1, ] == B[r2, ])) {
exclude_rows <- append(exclude_rows, r1)
}
}
}
values <- C[-exclude_rows, ]
} }
# TODO: add support for indices (if necessary) # TODO: add support for indices (if necessary)
return(values) return(values)

View file

@ -239,6 +239,7 @@ test_that("setdiff works as expected", {
Var2 = c('B', 'D'), Var2 = c('B', 'D'),
Var3 = c(TRUE, TRUE) Var3 = c(TRUE, TRUE)
) )
row.names(C) <- c(2L, 4L)
expect_equal(setdiff_MATLAB(A, B), C) # TODO: implement for data frames expect_equal(setdiff_MATLAB(A, B), C) # TODO: implement for data frames
# TODO: add more examples from https://se.mathworks.com/help/matlab/ref/double.setdiff.html;jsessionid=0d8d42582d4d299b8224403899f1 # TODO: add more examples from https://se.mathworks.com/help/matlab/ref/double.setdiff.html;jsessionid=0d8d42582d4d299b8224403899f1
}) })