duplicated
Determine Duplicate Elements

Description

duplicated determines which elements in a vector, a data frame, or an array are duplicates.
anyDuplicated finds the first element (the first row) that is a duplicate of another element (another 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 (the default), all values can be compared.
fromLast a logical flag. If TRUE, finds the duplicated values from the last to the head.
nmax a positive integer or NA. If a positive integer is given, and if x contains more than nmax unique values, then it is considered an error. (This argument can save memory when you are 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 are checked for duplicates.
  • 1 indicates rows.
  • 2 indicates columns.
  • c(1,2) indicates rows and columns.
... arguments for particular methods. Most methods call the default method and pass a possible nmax argument to it.

Details

The two functions are generic functions for vectors, data frames, and arrays (including matrices).
Value
duplicatedreturns a logical vector to indicate which elements are duplicates.
anyDuplicatedreturns 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 6.0.0-69
Package Index