<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Hi Carlos,</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">It’d be helpful to generate a MRE as to how you ended up with the data.table having a key set when it’s not really ordered properly.. Also, could you please test on level version as well (I don’t know the version you’re running on)?</div> <br> <div id="bloop_sign_1424733694798324992" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Arun</div></div> <br><p style="color:#000;">On 22 Feb 2015 at 00:41:51, Carlos Alberto Arnillas (<a href="mailto:carlosalberto.arnillas@gmail.com">carlosalberto.arnillas@gmail.com</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>Hello
<br>I am running the last version of R and data.table, however, I found a
<br>problem that I think has been reported for previous versions and I
<br>assumed it was fixed.
<br>
<br>Here is the data (as obtained from dput from a larger code)
<br>yy1 <- structure(list(Spp = c("vicr", "festuca"),
<br> rel_cover = c(0.0365853658536585,
<br>0.0609756097560976)),
<br> row.names = c(NA, -2L), class =
<br>c("data.table", "data.frame"),
<br> .Names = c("Spp", "rel_cover"))
<br>
<br>yy2 <- structure(list(Spp = c("eugra", "vicr", "festuca"),
<br> rel_cover = c(0.048780487804878,
<br>0.0609756097560976, 0.0975609756097561)),
<br> row.names = c(NA, -3L),
<br> class = c("data.table", "data.frame"),
<br> .Names = c("Spp", "rel_cover"), sorted = "Spp")
<br>> yy2
<br> Spp rel_cover
<br>1: eugra 0.04878049
<br>2: vicr 0.06097561
<br>3: festuca 0.09756098
<br>
<br>for some reason, the yy2 dataset had a key assigned (Spp) but wrongly
<br>applied (in fact, I never sort that dataset or the one that I used to
<br>create it using that variable). Then, if I try to merge both, I get a
<br>wrong result:
<br>
<br>> merge(yy1,yy2, by="Spp",all=T)
<br> Spp rel_cover.x rel_cover.y
<br>1: eugra NA 0.04878049
<br>2: festuca 0.06097561 NA
<br>3: festuca NA 0.09756098
<br>4: vicr 0.03658537 0.06097561
<br>
<br>however, if I set the key for each variable, I first get a warning,
<br>and then the right result
<br>
<br>> setkey(yy1, Spp)
<br>> setkey(yy2, Spp)
<br>Warning message:
<br>In setkeyv(x, cols, verbose = verbose, physical = physical) :
<br> Already keyed by this key but had invalid row order, key rebuilt. If
<br>you didn't go under the hood please let datatable-help know so the
<br>root cause can be fixed.
<br>
<br>
<br>> merge(yy1,yy2, by="Spp",all=T)
<br> Spp rel_cover.x rel_cover.y
<br>1: eugra NA 0.04878049
<br>2: festuca 0.06097561 0.09756098
<br>3: vicr 0.03658537 0.06097561
<br>
<br>
<br>To solve temporally the problem, I am using merge.data.frame, but I
<br>would prefer to keep all my data in data.table
<br>
<br>If it is not a bug, and I can do something to fix it, let me know please.
<br>
<br>Thanks in advance
<br>
<br>Carlos Alberto
<br>_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
<br></div></div></span></blockquote></body></html>