-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setorder()
on data.frame input order only the sort column
#5379
Comments
setorder()
on data.frame input order only the sort column
Cluing onto why: test_order_mod <- function(x) {
data.table::setDT(x)
x[, score:=score*1]
data.table::setorder(x, score)
}
x0 <- data.frame(id = 1:3, score = c(20, 10, 30))
x1 <- test_order_mod(x0) keeps From the reference semantics vignette, and my understanding of how R stores columns in a On a limb, I'd say that the advice from the vignette applies for the set* functions as well:
|
That's not a fix, unfortunately: x0 <- data.frame(id = 1:3, score = c(20, 10, 30), letters = c('a', 'b', 'c'))
x1 <- test_order_mod(x0)
x0
# id score letters
# 1: 1 10 b
# 2: 2 20 a
# 3: 3 30 c
The issue arises because One might envision a mechanism to track down such changes, but I'm not sure it's worth the overhead and complexity. More immediate, maybe we can move up the altrep expansion or delay the over allocation. But neither of those would alleviate the issue that you can't add or remove columns and have the result visible outside the function environment. For now, the best way around this would be to either use TL,DR: Don't use |
I think only Matt and Arun can merge on the master repository. |
I'm passing a data.frame to a function that sort it using
data.table::setorder
. Before sorting, I'm callingdata.table::setDT
on the input (in actuality I need to later call some specific data.table functions on the input). This is the function I'm using:and this is how I'm calling it:
The results seem strange:
x0
is changed to a data.table - that seems expected;x0$id
is not modified - it's address (usingdata.table::address
) remains the same, and it also remains in the original order;x0$score
also remains at the same address, but it is now sorted, so it's out of sync with the id column;x1
has the rows sorted correctly; also, the score column is at the same address as the score column ofx0
, but theid
column is, of course, at a different address. Here is what I get:Minimal reproducible example
Output of
sessionInfo()
The text was updated successfully, but these errors were encountered: