Why does data.table lose class definition in .SD after group by? <p>I've tried to attach my own class to a <code>numeric</code>, to alter the output <code>format</code>. This works fine, but after I do a group <code>by</code> the class reverts back to numeric.</p> <p>Example: Define a new format function for my class:</p> <pre><code>format.myclass &lt;- function(x, ...){ paste("!!", x, "!!", sep = "") } </code></pre> <p>Then make a small <code>data.table</code> and change one of the columns to myclass:</p> <pre><code>&gt; DT &lt;- data.table(L = rep(letters[1:3],3), N = 1:9) &gt; setattr(DT$N, "class", "myclass") &gt; DT L N 1: a !!1!! 2: b !!2!! 3: c !!3!! 4: a !!4!! 5: b !!5!! 6: c !!6!! 7: a !!7!! 8: b !!8!! 9: c !!9!! </code></pre> <p>Now perform a group by and the N column reverts to integer:</p> <pre><code>&gt; DT[, .SD, by = L] L N 1: a 1 2: a 4 3: a 7 4: b 2 5: b 5 6: b 8 7: c 3 8: c 6 9: c 9 &gt; DT[, sapply(.SD, class), by = L] L V1 1: a integer 2: b integer 3: c integer </code></pre> <p>Any idea why?</p>