EVOLUTION-MANAGER
Edit File: copy.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: Copy an entire object</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 copy {data.table}"><tr><td>copy {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Copy an entire object </h2> <h3>Description</h3> <p>In <code>data.table</code> parlance, all <code>set*</code> functions change their input <em>by reference</em>. That is, no copy is made at all, other than temporary working memory, which is as large as one column. The only other <code>data.table</code> operator that modifies input by reference is <code><a href="assign.html">:=</a></code>. Check out the <code>See Also</code> section below for other <code>set*</code> function <code>data.table</code> provides. </p> <p><code>copy()</code> copies an entire object. </p> <h3>Usage</h3> <pre> copy(x) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p> A <code>data.table</code>. </p> </td></tr> </table> <h3>Details</h3> <p><code>data.table</code> provides functions that operate on objects <em>by reference</em> and minimise full object copies as much as possible. Still, it might be necessary in some situations to work on an object's copy which can be done using <code>DT.copy <- copy(DT)</code>. It may also be sometimes useful before <code>:=</code> (or <code>set</code>) is used to subassign to a column by reference. </p> <p>A <code>copy()</code> may be required when doing <code>dt_names = names(DT)</code>. Due to R's <em>copy-on-modify</em>, <code>dt_names</code> still points to the same location in memory as <code>names(DT)</code>. Therefore modifying <code>DT</code> <em>by reference</em> now, say by adding a new column, <code>dt_names</code> will also get updated. To avoid this, one has to <em>explicitly</em> copy: <code>dt_names <- copy(names(DT))</code>. </p> <h3>Value</h3> <p>Returns a copy of the object. </p> <h3>See Also</h3> <p><code><a href="data.table.html">data.table</a></code>, <code><a href="setkey.html">setkey</a></code>, <code><a href="setDT.html">setDT</a></code>, <code><a href="setDF.html">setDF</a></code>, <code><a href="assign.html">set</a></code> <code><a href="assign.html">:=</a></code>, <code><a href="setorder.html">setorder</a></code>, <code><a href="setattr.html">setattr</a></code>, <code><a href="setattr.html">setnames</a></code> </p> <h3>Examples</h3> <pre> # Type 'example(copy)' to run these at prompt and browse output DT = data.table(A=5:1,B=letters[5:1]) DT2 = copy(DT) # explicit copy() needed to copy a data.table setkey(DT2,B) # now just changes DT2 identical(DT,DT2) # FALSE. DT and DT2 are now different tables DT = data.table(A=5:1, B=letters[5:1]) nm1 = names(DT) nm2 = copy(names(DT)) DT[, C := 1L] identical(nm1, names(DT)) # TRUE, nm1 is also changed by reference identical(nm2, names(DT)) # FALSE, nm2 is a copy, different from names(DT) </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>