<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;">It shouldn’t retain key if the subset results in reordering. So if you could provide an example that reproduces the key retaining, that’d be great! Also, please test it on 1.9.5 (current level version) to make sure it’s not fixed yet. Thanks.</div> <br> <div id="bloop_sign_1424734312639523072" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Arun</div></div> <br><p style="color:#000;">On 24 Feb 2015 at 00:31:30, 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>Hi.
<br>The version is 1.9.4.
<br>About how I ended up with a table not properly sorted? It happened
<br>because that table is a small subset (in terms of rows and columns) of
<br>a larger table, and the key used for the larger one include that
<br>variable as a third column. So, I guess that the new table inherit the
<br>key only for the columns that are in its subset, but it didn't rebuild
<br>the index, so the table end up unsorted...
<br>
<br>Carlos Alberto
<br>
<br>On Mon, Feb 23, 2015 at 6:23 PM, Arunkumar Srinivasan
<br><aragorn168b@gmail.com> wrote:
<br>> Hi Carlos,
<br>>
<br>> It’d be helpful to generate a MRE as to how you ended up with the data.table
<br>> having a key set when it’s not really ordered properly.. Also, could you
<br>> please test on level version as well (I don’t know the version you’re
<br>> running on)?
<br>>
<br>> --
<br>> Arun
<br>>
<br>> On 22 Feb 2015 at 00:41:51, Carlos Alberto Arnillas
<br>> (carlosalberto.arnillas@gmail.com) wrote:
<br>>
<br>> 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>