Counting-Generalization of duplicated()
Duplicated.RdDuplicated() generalizes the duplicated method for
vectors, by returning indices of “equivalence classes” for
duplicated entries and returning nomatch (NA by default)
for unique entries.
Note that duplicated() is not TRUE for the first time a
duplicate appears, whereas Duplicated() only marks unique
entries with nomatch (NA).
Arguments
- v
a vector, often character, factor, or numeric.
- incomparables
a vector of values that cannot be compared, passed to both
duplicated()andmatch().FALSEis a special value, meaning that all values can be compared, and may be the only value accepted for methods other than the default. It will be coerced internally to the same type asx.- fromLast
logical indicating if duplication should be considered from the reverse side, i.e., the last (or rightmost) of identical elements would correspond to
duplicated=FALSE.- nomatch
passed to
match(): the value to be returned in the case when no match is found. Note that it is coerced tointeger.
Value
an integer vector of the same length as v. Can be used as a
factor, e.g., in split,
tapply, etc.
See also
uniqueL (also in this sfsmisc package);
duplicated, match.
Examples
x <- c(9:12, 1:4, 3:6, 0:7)
data.frame(x, dup = duplicated(x),
dupL= duplicated(x, fromLast=TRUE),
Dup = Duplicated(x),
DupL= Duplicated(x, fromLast=TRUE))
#> x dup dupL Dup DupL
#> 1 9 FALSE FALSE NA NA
#> 2 10 FALSE FALSE NA NA
#> 3 11 FALSE FALSE NA NA
#> 4 12 FALSE FALSE NA NA
#> 5 1 FALSE TRUE 3 1
#> 6 2 FALSE TRUE 4 2
#> 7 3 FALSE TRUE 1 3
#> 8 4 FALSE TRUE 2 4
#> 9 3 TRUE TRUE 1 3
#> 10 4 TRUE TRUE 2 4
#> 11 5 FALSE TRUE 7 7
#> 12 6 FALSE TRUE 8 8
#> 13 0 FALSE FALSE NA NA
#> 14 1 TRUE FALSE 3 1
#> 15 2 TRUE FALSE 4 2
#> 16 3 TRUE FALSE 1 3
#> 17 4 TRUE FALSE 2 4
#> 18 5 TRUE FALSE 7 7
#> 19 6 TRUE FALSE 8 8
#> 20 7 FALSE FALSE NA NA