Skip to content

Commit a243da4

Browse files
author
bart
committed
Make duplicated and unique work (fixes #2138)
1 parent 697ab24 commit a243da4

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ S3method(c,sfc)
2929
S3method(c,sfg)
3030
S3method(cbind,sf)
3131
S3method(dim,sgbp)
32+
S3method(duplicated,sf)
3233
S3method(format,bbox)
3334
S3method(format,crs)
3435
S3method(format,sfc)

R/sf.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,23 @@ as.data.frame.sf = function(x, ...) {
450450
NextMethod()
451451
}
452452

453+
#' @export
454+
duplicated.sf <- function(x, incomparables = FALSE, fromLast = FALSE, ...) {
455+
if (length(x) != 1L) {
456+
if (any(i <- vapply(x, is.factor, NA))) {
457+
for (j in names(i[i])) {
458+
x[[j]] <- lapply(x[[j]], as.numeric)
459+
}
460+
}
461+
if (any(i <- (lengths(lapply(x, dim)) == 2L))) {
462+
for (j in names(i[i])) {
463+
x[[j]] <- lapply(x[[j]], split.data.frame, seq_len(nrow(x)))
464+
}
465+
}
466+
}
467+
NextMethod()
468+
}
469+
453470
#' @export
454471
#' @name st_geometry
455472
#' @details if \code{x} is of class \code{sf}, \code{st_drop_geometry} drops the geometry of its argument, and reclasses it accordingly; otherwise it returns \code{x} unmodified.

tests/testthat/test_sf.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ test_that("empty agr attribute is named after subset", {
9999
agr = attr(out, "agr")
100100
expect_identical(names(agr), character())
101101
})
102+
test_that("duplicated work",{
103+
sf = st_sf(data.frame(x = st_sfc(st_point(1:2))[rep(1,4)], a=gl(2,2), b=as.numeric(gl(2,2))))
104+
expect_identical(duplicated(sf), c(FALSE,TRUE,FALSE,TRUE))
105+
expect_s3_class(unique(sf),'sf')
106+
})

0 commit comments

Comments
 (0)