title: R function to create a new variable in a data table converting YYYYMMDD to a real date
body: <p>I wanted use lapply to build an utility that converts the date format as below described.
  What should be fixed?</p>
  <pre><code>library(data.table)
  testYYYYMMDD &lt;- c(20200321, 20191231, 20230630)
  test &lt;- as.data.table(testYYYYMMDD)
  
  ToDate &lt;- function(Data, DateYYYYMMDD){
      if (any(is.na(Data[,DateYYYYMMDD]))){warning(&quot;Some dates are NA&quot;)}
      Data[, NewColumn := lapply(.SD, function(x) 
      YYYY &lt;- trunc(x/10000,0)
      MM &lt;- trunc(x/100,0)-YYYY*100 
      DD &lt;- x-(YYYY*10000+MM*100)
      NewColumn &lt;- as.Date((paste(YYYY, MM, DD, sep=&quot;-&quot;)), &quot;%Y-%m-%d&quot;)
      ), .SDcols = NewColumn]
    return(Data)}
  
  ToDate(test, testYYYMMDD)
  </code></pre>