Skip to content

Base R generic 'sort_by' for data.tables #6662

@rikivillalba

Description

@rikivillalba

I wonder whether it is worth to implement sort_by.data.table (R 4.4.0) with internal forder instead of base order, and possibly an 'in_place' argument

Some improvement can be seen in my little test:

library(data.table)
set.seed(1234)
DT <- data.table(x=runif(1e7),y=runif(1e7))

# sort_by.data.frame with "forder" instead of "order"
sort_by2 <- function (x, y, ...) 
{
  if (inherits(y, "formula")) 
    y <- .formula2varlist(y, x)
  if (!is.list(y)) 
    y <- list(y)
  o <- do.call(data.table:::forder, c(unname(y), list(...)))
  x[o, , drop = FALSE]
}

microbenchmark::microbenchmark(times = 1, 
  sort_by2(DT, ~ x + y),
  sort_by(DT, ~ x + y)
)
#> Unit: seconds
#>                  expr      min       lq     mean   median       uq      max
#>  sort_by2(DT, ~x + y) 1.068101 1.068101 1.068101 1.068101 1.068101 1.068101
#>   sort_by(DT, ~x + y) 2.317193 2.317193 2.317193 2.317193 2.317193 2.317193

Created on 2024-12-12 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions