duplicated
Determine Duplicate Elements

Description

duplicated determines which elements in a vector, a data frame or array are duplicates. anyDuplicated finds the first elemnt (row) which is a duplicate of another element (row)

Usage

duplicated(x, incomparables = FALSE, ...)
## Default S3 method:
duplicated(x, incomparables = FALSE, fromLast = FALSE, nmax = NA, ...)
## S3 method for class 'data.frame':
duplicated(x, incomparables = FALSE, fromLast = FALSE, ...)
## S3 method for class 'matrix':
duplicated(x, incomparables = FALSE, MARGIN = 1L, fromLast = FALSE, ...)
## S3 method for class 'array':
duplicated(x, incomparables = FALSE, MARGIN = 1L, fromLast = FALSE, ...)
## S3 method for class 'POSIXlt':
duplicated(x, incomparables = FALSE, ...)

anyDuplicated(x, incomparables = FALSE, ...) ## Default S3 method: anyDuplicated(x, incomparables = FALSE, fromLast = FALSE, ...) ## S3 method for class 'array': anyDuplicated(x, incomparables = FALSE, MARGIN = 1L, fromLast = FALSE, ...) ## S3 method for class 'matrix': anyDuplicated(x, incomparables = FALSE, MARGIN = 1L, fromLast = FALSE, ...) ## S3 method for class 'data.frame': anyDuplicated(x, incomparables = FALSE, fromLast = FALSE, ...)

Arguments

x a vector, a data frame or an array. Missing values and Infs are allowed.

incomparables a vector of values that cannot be compared, each value in x that matches a value in this vector is considered unique. If incomparables=FALSE all values can be compared.
fromLast a logical flag, if TRUE, judge the duplicated values from the last to the head.
nmax a positive integer or NA. If a positive integer is given then it is considered an error if x contains more than nmax unique values. (This can save memory when working with very long vectors and you are looking for cases with only a few unique values.)
MARGIN an integer vector to indicate which subscripts of the matrix or array will be checked for duplicated. 1 indicates rows, 2 indicates columns, c(1,2) indicates rows and columns.
... arguments for particular methods. Most methods will call the default method and they will pass a possible nmax argument to it.

Details

The two functions are generic function for vectors, data frames and arrays(including matrices).
In anyDuplicated if no element is duplicated, it will return 0. If fromLast is TRUE, then compare starts from the end.
Value
duplicated returns a logical vector to indicate which elements are duplicates.
anyDuplicated returns the position of the first duplicated element, or 0 if there were no duplcates.
See Also
unique, POSIXt
Examples
duplicated(c(1,2,1))
duplicated(c(1,2,1), incomparables=TRUE)
duplicated(c(1,2,1), fromLast=TRUE)
duplicated(matrix(c(1,1,2,2,3,3), nrow=2), MARGIN=1)
duplicated(matrix(c(1,1,2,2,3,3), nrow=2), MARGIN=2)
duplicated(matrix(c(1,1,2,2,3,3), nrow=2), MARGIN=c(1, 2))
anyDuplicated(c(1,2,3,4), fromLast=TRUE)
anyDuplicated(c(1,2,3,3), fromLast=FALSE)
anyDuplicated(c(1,2,3,3), fromLast=TRUE)

## POSIXlt x <- as.POSIXlt("1970-01-01") duplicated(c(x, x+1, NA, x, x+1, NA))

tryCatch(sum(duplicated(1:1e7, nmax=100)), error=function(e)NA_integer_) tryCatch(sum(duplicated(rep(1,1e7), nmax=100)), error=function(e)NA_integer_)

Package base version 4.0.0-28
Package Index