Skip to content

setorderv could take index vector too #4012

@jangorecki

Description

@jangorecki

It was requested at least twice
https://stackoverflow.com/questions/37878620/reorder-rows-in-data-table-in-a-specific-order/
https://stackoverflow.com/questions/56759013/is-there-are-version-of-setorder-that-behaves-like-setcolorder
I posted an answer in one of the questions as a new function which uses data.table internals

setroworder <- function(x, neworder) {
    .Call(data.table:::Creorder, x, as.integer(neworder), PACKAGE = "data.table")
    invisible(x)
}

Instead of new function we could just add new argument neworder that would redirect to this functionality.

It came back to me just now when needed to randomly reorder data.
Having big DT and doing DT[sample(.N)] is costly because of copy. Doing setorder(DT, neworder=sample(nrow(DT))) would be much nicer.


another not bad way is to

set(DT, NULL, "i", sample(nrow(DT)))
setorderv(DT, "i")
set(DT, NULL, "i", NULL)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions