Skip to content

Commit a793cf9

Browse files
author
maechler
committed
unique(<difftime>) now works, too
git-svn-id: https://svn.r-project.org/R/trunk@87667 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent c8d58f9 commit a793cf9

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

doc/NEWS.Rd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@
188188
189189
\item A \code{summary()} method for \code{"difftime"} objects which
190190
prints nicely, similar to those for \code{"Date"} and \code{"POSIXct"}.
191+
192+
\item \code{unique()}'s default method now also deals with
193+
\code{"difftime"} objects.
191194
}
192195
}
193196

src/library/base/R/duplicated.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/base/R/duplicated.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2018 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -120,9 +120,11 @@ function(x, incomparables = FALSE, fromLast = FALSE, nmax = NA, ...)
120120
}
121121
z <- .Internal(unique(x, incomparables, fromLast, nmax))
122122
if(inherits(x, "POSIXct"))
123-
structure(z, class = class(x), tzone = attr(x, "tzone"))
123+
.POSIXct(z, attr(x, "tzone"), class(x))
124124
else if(inherits(x, "Date"))
125-
structure(z, class = class(x))
125+
.Date(z, class(x))
126+
else if(inherits(x, "difftime"))
127+
.difftime(z, attr(x,"units"), class(x))
126128
else z
127129
}
128130

src/library/base/man/unique.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ unique(x, incomparables = FALSE, \dots)
7979
For a vector, an object of the same type of \code{x}, but with only
8080
one copy of each duplicated element. No attributes are copied (so
8181
the result has no names).
82+
The default method also keeps (number-like) date-time vectors of classes
83+
\code{"\link{POSIXct}"}, \code{"\link{Date}"}, and \code{"\link{difftime}"}.
8284

8385
For a data frame, a data frame is returned with the same columns but
8486
possibly fewer rows (and with row names from the first occurrences of

tests/reg-tests-1e.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,12 @@ stopifnot(exprs = {
18001800
## summary(<difftime>) was not useful in R < 4.5.0
18011801

18021802

1803+
## unique(<difftime>)
1804+
(unidt <- unique(dt))
1805+
stopifnot(inherits(unidt, "difftime"), length(unidt) <= 2) # '2': allow "inaccuracy"
1806+
## unique() lost the class in R < 4.5.0
1807+
1808+
18031809

18041810
## keep at end
18051811
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)