title: R data.table: how to go from tibble to data.table to tibble back?
body: <p>I use mainly tables in the <em>tibble</em> fromat from <code>tidyverse</code>, but for some steps, I use the <code>data.table</code> package. I want to see what is the best way of converting a <em>data.table</em> back to <em>tibble</em>? </p>
  
  <p>I understand that <code>data.table</code> has some clever function <a href="https://rdrr.io/rforge/data.table/man/setDT.html" rel="noreferrer">setDT</a> and <a href="https://rdrr.io/rforge/data.table/man/setDF.html" rel="noreferrer">setDF</a> function, that convert from <em>data.frame</em> to <em>data.table</em> (and vice-versa) by reference, i.e. without making a copy. </p>
  
  <p>But what if I wanted to convert back to <em>tibble</em>? Am I copying the data using <code>as_tibble</code> on the <em>data.frame</em> resulting from <code>setDT()</code>? Is there a clever way to use this, maybe using the <code>setattr()</code> from <code>data.table</code>?</p>
  
  <pre><code>library(data.table)
  library(tidyverse)
  
  iris_tib &lt;- as_tibble(iris)
  
  ## some data.table operation
  setDT(iris_tib)
  setkey(iris_tib, Species)
  iris_tib[, Sepal.Length.Mean := mean(Sepal.Length), by = Species]
  
  
  
  ## How to convert back to tibble efficiently?
  setDF(iris_tib)
  iris_tib_back &lt;-  as_tibble(iris_tib)
  
  ## it looks like we were able to update by reference? Only rownames were (shallow) copied?
  changes(iris_tib, iris_tib_back)
  </code></pre>