<div dir="ltr"><div>I'm a noob to data.table and I've got a couple of questions:<br><br></div>1). Why do I get different answers in the following example:<br><br><pre class="" tabindex="0"><span class="">> </span><span class="">DT = data.table(a=c(4:13),y=c(1,1,2,2,2,3,3,3,4,4),x=1:10,z=c(1,1,1,1,2,2,2,2,3,3),zz=c(1,1,1,1,1,2,2,2,2,2))
</span><span class="">> </span><span class="">setkeyv(DT,cols=c("a","x","y","z","zz"))
</span><span class="">> </span><span class="">DT[,if(.N>=4) {list(predict(smooth.spline(x,y),<b><span style="background-color:rgb(255,0,0)">c(4,5,6)</span></b>)$y)} ,by=z]
</span> z V1
1: 1 2.1000000
2: 1 2.5000000
3: 1 2.9000000
4: 2 0.9998959
5: 2 2.0453352
6: 2 2.9093247<br><br></pre><pre class="" tabindex="0">Versus:<br><br><span class="">> </span><span class="">DT[,if(.N>=4) {list(predict(smooth.spline(x,y),<span style="background-color:rgb(255,0,0)"><b>a[1:3]</b></span>)$y)} ,by=z]
</span> z V1
1: 1 2.100000
2: 1 2.500000
3: 1 2.900000
4: 2 2.999995
5: 2 2.954664
6: 2 2.909333<br><br></pre><pre class="" tabindex="0">Is some sort of recycling going on here?<br></pre><pre class="" tabindex="0"><br></pre><pre class="" tabindex="0"><span style="font-family:arial black,sans-serif"><span style="font-family:arial,helvetica,sans-serif">2). </span> <span style="font-family:arial,helvetica,sans-serif">How can I do some sort of nested "by" statement? <br>
Let's say I want to set by=zz, but run the spline statement within each z subset. Do I use .SD somehow?</span><br><br></span></pre><pre class="" tabindex="0"><span style="font-family:arial black,sans-serif"><span style="font-family:arial,helvetica,sans-serif">This is great package - it's just taking me some time to get the syntax right. I've found this to be faster than clusterMap on 2 cores...<br>
I hope I've used the correct terminology!</span><br><br></span></pre><pre class="" tabindex="0"><span style="font-family:arial,helvetica,sans-serif">Best,<br><br></span></pre><pre class="" tabindex="0"><span style="font-family:arial black,sans-serif"><span style="font-family:arial,helvetica,sans-serif">John</span> </span><br>
</pre><pre class="" tabindex="0"><br><br></pre><br></div>