EVOLUTION-MANAGER
Edit File: rep.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: Replicate Elements of Vectors and Lists</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 rep {base}"><tr><td>rep {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Replicate Elements of Vectors and Lists</h2> <h3>Description</h3> <p><code>rep</code> replicates the values in <code>x</code>. It is a generic function, and the (internal) default method is described here. </p> <p><code>rep.int</code> and <code>rep_len</code> are faster simplified versions for two common cases. Internally, they are generic, so methods can be defined for them. </p> <h3>Usage</h3> <pre> rep(x, ...) rep.int(x, times) rep_len(x, length.out) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a vector (of any mode including a <code><a href="list.html">list</a></code>) or a factor or (for <code>rep</code> only) a <code>POSIXct</code> or <code>POSIXlt</code> or <code>Date</code> object; or an S4 object containing such an object.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments to be passed to or from other methods. For the internal default method these can include: </p> <dl> <dt><code>times</code></dt><dd><p>an integer-valued vector giving the (non-negative) number of times to repeat each element if of length <code>length(x)</code>, or to repeat the whole vector if of length 1. Negative or <code>NA</code> values are an error. A <code>double</code> vector is accepted, other inputs being coerced to an integer or double vector.</p> </dd> <dt><code>length.out</code></dt><dd><p>non-negative integer. The desired length of the output vector. Other inputs will be coerced to a double vector and the first element taken. Ignored if <code>NA</code> or invalid.</p> </dd> <dt><code>each</code></dt><dd><p>non-negative integer. Each element of <code>x</code> is repeated <code>each</code> times. Other inputs will be coerced to an integer or double vector and the first element taken. Treated as <code>1</code> if <code>NA</code> or invalid.</p> </dd> </dl> </td></tr> <tr valign="top"><td><code>times, length.out</code></td> <td> <p>see <code>...</code> above.</p> </td></tr> </table> <h3>Details</h3> <p>The default behaviour is as if the call was </p> <pre> rep(x, times = 1, length.out = NA, each = 1) </pre><p>. Normally just one of the additional arguments is specified, but if <code>each</code> is specified with either of the other two, its replication is performed first, and then that implied by <code>times</code> or <code>length.out</code>. </p> <p>If <code>times</code> consists of a single integer, the result consists of the whole input repeated this many times. If <code>times</code> is a vector of the same length as <code>x</code> (after replication by <code>each</code>), the result consists of <code>x[1]</code> repeated <code>times[1]</code> times, <code>x[2]</code> repeated <code>times[2]</code> times and so on. </p> <p><code>length.out</code> may be given in place of <code>times</code>, in which case <code>x</code> is repeated as many times as is necessary to create a vector of this length. If both are given, <code>length.out</code> takes priority and <code>times</code> is ignored. </p> <p>Non-integer values of <code>times</code> will be truncated towards zero. If <code>times</code> is a computed quantity it is prudent to add a small fuzz or use <code><a href="Round.html">round</a></code>. And analogously for <code>each</code>. </p> <p>If <code>x</code> has length zero and <code>length.out</code> is supplied and is positive, the values are filled in using the extraction rules, that is by an <code>NA</code> of the appropriate class for an atomic vector (<code>0</code> for raw vectors) and <code>NULL</code> for a list. </p> <h3>Value</h3> <p>An object of the same type as <code>x</code>. </p> <p><code>rep.int</code> and <code>rep_len</code> return no attributes (except the class if returning a factor). </p> <p>The default method of <code>rep</code> gives the result names (which will almost always contain duplicates) if <code>x</code> had names, but retains no other attributes. </p> <h3>Note</h3> <p>Function <code>rep.int</code> is a simple case which was provided as a separate function partly for S compatibility and partly for speed (especially when names can be dropped). The performance of <code>rep</code> has been improved since, but <code>rep.int</code> is still at least twice as fast when <code>x</code> has names. </p> <p>The name <code>rep.int</code> long precedes making <code>rep</code> generic. </p> <p>Function <code>rep</code> is a primitive, but (partial) matching of argument names is performed as for normal functions. </p> <p>For historical reasons <code>rep</code> (only) works on <code>NULL</code>: the result is always <code>NULL</code> even when <code>length.out</code> is positive. </p> <p>Although it has never been documented, these functions have always worked on <a href="expression.html">expression</a> vectors. </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="seq.html">seq</a></code>, <code><a href="sequence.html">sequence</a></code>, <code><a href="lapply.html">replicate</a></code>. </p> <h3>Examples</h3> <pre> rep(1:4, 2) rep(1:4, each = 2) # not the same. rep(1:4, c(2,2,2,2)) # same as second. rep(1:4, c(2,1,2,1)) rep(1:4, each = 2, len = 4) # first 4 only. rep(1:4, each = 2, len = 10) # 8 integers plus two recycled 1's. rep(1:4, each = 2, times = 3) # length 24, 3 complete replications rep(1, 40*(1-.8)) # length 7 on most platforms rep(1, 40*(1-.8)+1e-7) # better ## replicate a list fred <- list(happy = 1:10, name = "squash") rep(fred, 5) # date-time objects x <- .leap.seconds[1:3] rep(x, 2) rep(as.POSIXlt(x), rep(2, 3)) ## named factor x <- factor(LETTERS[1:4]); names(x) <- letters[1:4] x rep(x, 2) rep(x, each = 2) rep.int(x, 2) # no names rep_len(x, 10) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>