setorderv could take index vector too

Open
#4,012 1 comment 0 reactions 1 assignee View on GitHub

@jangorecki is already working on this.

Since Apr 2, 2020.

  • #4340 by @jangorecki — open

Assessment

This issue has not been assessed yet.

Description

feature request

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)
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.