Skip to content

Commit

Permalink
unique(<difftime>) now works, too
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@87667 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
maechler committed Jan 29, 2025
1 parent c8d58f9 commit a793cf9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@
\item A \code{summary()} method for \code{"difftime"} objects which
prints nicely, similar to those for \code{"Date"} and \code{"POSIXct"}.
\item \code{unique()}'s default method now also deals with
\code{"difftime"} objects.
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/library/base/R/duplicated.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# File src/library/base/R/duplicated.R
# Part of the R package, https://www.R-project.org
#
# Copyright (C) 1995-2018 The R Core Team
# Copyright (C) 1995-2025 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -120,9 +120,11 @@ function(x, incomparables = FALSE, fromLast = FALSE, nmax = NA, ...)
}
z <- .Internal(unique(x, incomparables, fromLast, nmax))
if(inherits(x, "POSIXct"))
structure(z, class = class(x), tzone = attr(x, "tzone"))
.POSIXct(z, attr(x, "tzone"), class(x))
else if(inherits(x, "Date"))
structure(z, class = class(x))
.Date(z, class(x))
else if(inherits(x, "difftime"))
.difftime(z, attr(x,"units"), class(x))
else z
}

Expand Down
2 changes: 2 additions & 0 deletions src/library/base/man/unique.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ unique(x, incomparables = FALSE, \dots)
For a vector, an object of the same type of \code{x}, but with only
one copy of each duplicated element. No attributes are copied (so
the result has no names).
The default method also keeps (number-like) date-time vectors of classes
\code{"\link{POSIXct}"}, \code{"\link{Date}"}, and \code{"\link{difftime}"}.

For a data frame, a data frame is returned with the same columns but
possibly fewer rows (and with row names from the first occurrences of
Expand Down
6 changes: 6 additions & 0 deletions tests/reg-tests-1e.R
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,12 @@ stopifnot(exprs = {
## summary(<difftime>) was not useful in R < 4.5.0


## unique(<difftime>)
(unidt <- unique(dt))
stopifnot(inherits(unidt, "difftime"), length(unidt) <= 2) # '2': allow "inaccuracy"
## unique() lost the class in R < 4.5.0



## keep at end
rbind(last = proc.time() - .pt,
Expand Down

0 comments on commit a793cf9

Please sign in to comment.