EVOLUTION-MANAGER
Edit File: duplicated.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Determine Duplicate Elements</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for duplicated {base}"><tr><td>duplicated {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Determine Duplicate Elements</h2> <h3>Description</h3> <p><code>duplicated()</code> determines which elements of a vector or data frame are duplicates of elements with smaller subscripts, and returns a logical vector indicating which elements (rows) are duplicates. </p> <p><code>anyDuplicated(.)</code> is a “generalized” more efficient shortcut for <code>any(duplicated(.))</code>. </p> <h3>Usage</h3> <pre> duplicated(x, incomparables = FALSE, ...) ## Default S3 method: duplicated(x, incomparables = FALSE, fromLast = FALSE, nmax = NA, ...) ## S3 method for class 'array' duplicated(x, incomparables = FALSE, MARGIN = 1, fromLast = FALSE, ...) anyDuplicated(x, incomparables = FALSE, ...) ## Default S3 method: anyDuplicated(x, incomparables = FALSE, fromLast = FALSE, ...) ## S3 method for class 'array' anyDuplicated(x, incomparables = FALSE, MARGIN = 1, fromLast = FALSE, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a vector or a data frame or an array or <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>incomparables</code></td> <td> <p>a vector of values that cannot be compared. <code>FALSE</code> is 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 as <code>x</code>.</p> </td></tr> <tr valign="top"><td><code>fromLast</code></td> <td> <p>logical indicating if duplication should be considered from the reverse side, i.e., the last (or rightmost) of identical elements would correspond to <code>duplicated = FALSE</code>.</p> </td></tr> <tr valign="top"><td><code>nmax</code></td> <td> <p>the maximum number of unique items expected (greater than one).</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>arguments for particular methods.</p> </td></tr> <tr valign="top"><td><code>MARGIN</code></td> <td> <p>the array margin to be held fixed: see <code><a href="apply.html">apply</a></code>, and note that <code>MARGIN = 0</code> may be useful.</p> </td></tr> </table> <h3>Details</h3> <p>These are generic functions with methods for vectors (including lists), data frames and arrays (including matrices). </p> <p>For the default methods, and whenever there are equivalent method definitions for <code>duplicated</code> and <code>anyDuplicated</code>, <code>anyDuplicated(x, ...)</code> is a “generalized” shortcut for <code>any(duplicated(x, ...))</code>, in the sense that it returns the <em>index</em> <code>i</code> of the first duplicated entry <code>x[i]</code> if there is one, and <code>0</code> otherwise. Their behaviours may be different when at least one of <code>duplicated</code> and <code>anyDuplicated</code> has a relevant method. </p> <p><code>duplicated(x, fromLast = TRUE)</code> is equivalent to but faster than <code>rev(duplicated(rev(x)))</code>. </p> <p>The array method calculates for each element of the sub-array specified by <code>MARGIN</code> if the remaining dimensions are identical to those for an earlier (or later, when <code>fromLast = TRUE</code>) element (in row-major order). This would most commonly be used to find duplicated rows (the default) or columns (with <code>MARGIN = 2</code>). Note that <code>MARGIN = 0</code> returns an array of the same dimensionality attributes as <code>x</code>. </p> <p>Missing values (<code>"<a href="NA.html">NA</a>"</code>) are regarded as equal, numeric and complex ones differing from <code>NaN</code>; character strings will be compared in a “common encoding”; for details, see <code><a href="match.html">match</a></code> (and <code><a href="unique.html">unique</a></code>) which use the same concept. </p> <p>Values in <code>incomparables</code> will never be marked as duplicated. This is intended to be used for a fairly small set of values and will not be efficient for a very large set. </p> <p>Except for factors, logical and raw vectors the default <code>nmax = NA</code> is equivalent to <code>nmax = length(x)</code>. Since a hash table of size <code>8*nmax</code> bytes is allocated, setting <code>nmax</code> suitably can save large amounts of memory. For factors it is automatically set to the smaller of <code>length(x)</code> and the number of levels plus one (for <code>NA</code>). If <code>nmax</code> is set too small there is liable to be an error: <code>nmax = 1</code> is silently ignored. </p> <p><a href="LongVectors.html">Long vectors</a> are supported for the default method of <code>duplicated</code>, but may only be usable if <code>nmax</code> is supplied. </p> <h3>Value</h3> <p><code>duplicated()</code>: For a vector input, a logical vector of the same length as <code>x</code>. For a data frame, a logical vector with one element for each row. For a matrix or array, and when <code>MARGIN = 0</code>, a logical array with the same dimensions and dimnames. </p> <p><code>anyDuplicated()</code>: an integer or real vector of length one with value the 1-based index of the first duplicate if any, otherwise <code>0</code>. </p> <h3>Warning</h3> <p>Using this for lists is potentially slow, especially if the elements are not atomic vectors (see <code><a href="vector.html">vector</a></code>) or differ only in their attributes. In the worst case it is <i>O(n^2)</i>. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="unique.html">unique</a></code>.</p> <h3>Examples</h3> <pre> x <- c(9:20, 1:5, 3:7, 0:8) ## extract unique elements (xu <- x[!duplicated(x)]) ## similar, same elements but different order: (xu2 <- x[!duplicated(x, fromLast = TRUE)]) ## xu == unique(x) but unique(x) is more efficient stopifnot(identical(xu, unique(x)), identical(xu2, unique(x, fromLast = TRUE))) duplicated(iris)[140:143] duplicated(iris3, MARGIN = c(1, 3)) anyDuplicated(iris) ## 143 anyDuplicated(x) anyDuplicated(x, fromLast = TRUE) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>