<div dir="ltr">Hey Arun, <div><br></div><div>great call on using `alloc.col()` I would not have thought of that. </div><div><br></div><div>Since we were previously talking about updates to common functions in the package, I wouldnt mind seeing a arugment added to `unique.data.table` along the lines of `useKey=FALSE` (perhaps better named). Thoughts? </div>
<div><br></div><div class="gmail_extra"><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)"><div style="font-size:13px">Rick</div></div></div>
<br><br><div class="gmail_quote">On Wed, Jul 31, 2013 at 11:06 AM, Arunkumar Srinivasan <span dir="ltr"><<a href="mailto:aragorn168b@gmail.com" target="_blank">aragorn168b@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
Ricardo,
</div><div><br></div><div>Yes, I was also thinking of this, because of precisely the issue you mention. In this case, I'd do `invisible(alloc.col(DT2))` before assigning by reference. The typical way of converting from a data.frame to a data.table (without complete copy or rather with a "shallow" copy) is:</div>
<div><br></div><div>DF <- data.frame(x=1:5, y=6:10)</div><div>tracemem(DF)</div><div>[1] "<0x100f08678>"</div><div><br></div><div>setattr(DF, 'class', c('data.table', 'data.frame'))</div>
<div>data.table:::settruelength(DF, 0)</div><div>invisible(alloc.col(DF))</div><div>tracemem(DF)</div><div>[1] "<0x103c23b30>"</div><div><br></div><div>DF[, z := 1]</div><div><br></div><div>Even thought there's a copy happening, this, as I understand is a "shallow" copy (copying only references/pointers and not the entire data) and therefore should have almost negligible time in copying). Now, if you look at the second line, it first sets the "truelength" attribute to 0 (which is set to NULL for a data.frame, if you look at as.data.frame.data.table function). Then it allocates the columns with "alloc.col". So, </div>
<div><br></div><div>DT1 <- data.table(1)</div><div>DT2 <- unique.data.frame(DT1) # <~~~ your true length is screwed up</div><div>truelength(DT2)</div><div># [1] 0</div><div><br></div><div>invisible(alloc.col(DT2))</div>
<div>truelength(DT2)</div><div># [1] 100</div><div><br></div><div>DT2[, w := 2]</div><div># no warning / full copy.</div>
<div><div><br></div><div>So, Frank, I guess this is an alternate way if you don't want the warning/full copy, but you want to specifically use `unique.data.frame`.</div><div><br></div><div>Thanks for bringing it up Ricardo. If I've gotten something wrong, feel free to correct me..</div>
<div><br></div><div>Arun</div><div><br></div></div><div class="HOEnZb"><div class="h5">
<p style="color:#a0a0a8">On Wednesday, July 31, 2013 at 3:49 PM, Ricardo Saporta wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div>Arun, just to comment on this part: <div><br></div><div><<<font><span style="line-height:normal;background-color:rgba(255,255,255,0)">The answer to your problem is that you should be using `unique(DT1)` instead of `unique.data.frame(DT1)` because `unique` will call the "correct" `unique.data.table` method on DT1. </span></font>>></div>
<div><br></div><div>I use `unique.data.frame(DT)` all the time. </div><div>The reason being that I often have data with multiple rows per key. If I want all unique rows, `unique.data.table`<span></span> gives me a result other than what I need. Any thoughts on a better way? </div>
<div><br>On Wednesday, July 31, 2013, Arunkumar Srinivasan wrote:<br><blockquote type="cite"><div>
<div>Frank,
</div><div><br></div><div>The answer to your problem is that you should be using `unique(DT1)` instead of `unique.data.frame(DT1)` because `unique` will call the "correct" `unique.data.table` method on DT1. </div>
<div><br></div><div>Now, as to why this is happening… You should know that data.table over allocates a list of column pointers in order to add columns by reference (you can read about this more, if you wish, by looking at ?`:=`). That is, if you do:</div>
<div><br></div><div>DT1 <- data.table(1)</div><div><br></div><div>You've created 1 column. But you've (or data.table has) allocated vector of a 100 column pointers (by default). You can see this by using the function `truelength`.</div>
<div><br></div><div>truelength(DT1)</div><div>> 100</div><div><br></div><div>Your problem with `unique.data.frame` is that this `truelength` is not maintained after doing this copy. That is:</div><div><br></div><div>DT2 <- unique(DT1) # <~~~ correct way</div>
<div>DT3 <- unique.data.frame(DT1) # <~~~ incorrect way</div><div><br></div><div>truelength(DT2)</div><div>> 100</div><div>truelength(DT3)</div><div>> 0</div><div><br></div><div>Therefore, we've a problem now. The over-allocated memory is somehow "gone" after this copy. Therefore when you do a `:=` after this, we will be writing to a memory location which isn't allocated. And this would normally lead to a segmentation fault (IIUC). </div>
<div><br></div><div>And this is what happened with an earlier version of data.table in a similar context - setting the key of data.table. In version 1.7.8, the key of a data.table was set by:</div><div><br></div><div>key(DT) <- …</div>
<div><br></div><div>And this resulted in a "copy" that set the true length to 0. So assigning by reference after this step lead to a segmentation fault. This is why now we have a "setkey" function or more general "setattr" function to assign things without R's copy screwing things up.</div>
<div><br></div><div>In order to catch this issue and rectify it without throwing a segmentation fault, the attribute ".internal.selfref" was designed. Basically it finds these situations and in that case gets a copy before assigning by reference. I can't find a documentation on "how" it's done. But the way I think of it is that when you assign by reference the existing .internal.selfref attribute (which is of class externalptr) is compared with the actual value of your data.table and if they match, then everything's good. Else, it has to make a copy and set the correct ptr as the attribute.</div>
<div><br></div><div>You can read about this in ?setkey. So in essence use `unique` which'll call the correct `unique.data.table` (hidden) function. Hope this helps. If there's ambiguity or I got something wrong, please point out.</div>
<div><br></div><div>Arun</div><div><div><br></div></div>
<p style="color:#a0a0a8">On Wednesday, July 31, 2013 at 12:07 AM, Frank Erickson wrote:</p><blockquote type="cite"><div>
<span><div><div><div>I expect DT2 <- unique.data.frame(DT1) to be a new object, but get a warning about pointers, so apparently it is not...? </div><div><br></div><div>A short example:</div><div><br>
</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><div>DT1 <- data.table(1)</div></div><div><div>DT2 <- unique.data.frame(DT1)</div></div><div><div>DT2[,gah:=1]</div></div></blockquote><div><br></div><div>An example closer to my application, undoing a cartesian/cross join:</div>
<div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>DT1 <- CJ(A=0:1,B=1:6,D0=0:1,D=0:1)[D>=D0]</div></div><div><div>setkey(DT1,A)</div></div><div><div>DT2 <- unique.data.frame(DT1[,-which(names(DT1)%in%'B'),with=FALSE])</div>
</div><div><div>DT2[,gah:=1] # warning: I should have made a copy, apparently</div></div></blockquote><div><br></div><div>I'm fine with explicitly making a copy, of course, and don't really know anything about pointers. I just thought I'd bring it up.</div>
<div><br></div><div>--Frank</div>
</div><div><div>_______________________________________________</div><div>datatable-help mailing list</div><div><a>datatable-help@lists.r-forge.r-project.org</a></div>
<div><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a></div></div></div></span>
</div></blockquote><div>
<br>
</div>
</div></blockquote></div><br><br>-- <br><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif"><div style="font-size:13px">Ricardo Saporta</div><div style="font-size:13px">
Graduate Student, Data Analytics</div><div style="font-size:13px"><span style="font-size:13px">Rutgers University, New Jersey</span></div><div style="font-size:13px"><span style="font-size:13px">e: </span><a href="mailto:saporta@rutgers.edu" style="color:rgb(17,85,204);font-size:13px" target="_blank">saporta@rutgers.edu</a></div>
<div><br></div></div><br>
</div></div></span>
</blockquote>
<div>
<br>
</div>
</div></div></blockquote></div><br></div></div>