Skip to content

export is.sortedΒ #2325

@franknarf1

Description

@franknarf1

I'd like to test whether my input table is sorted and error out if not. The base tools for this are not very appealing.

For now, I have a workaround:

library(data.table)
is_sorted = function(DT, cols = names(DT)){
  e = as.call(lapply(c("order", cols), as.name))
  identical(seq_len(nrow(DT)), DT[eval(e), which = TRUE])
} 

DT = data.table(id = 1:3, v = c(2,1,3))
is_sorted(DT) # TRUE
is_sorted(DT, "v") # FALSE

This is similar to a couple answers on SO, but implicitly uses forder instead of order. I guess data.table:::is.sorted is more efficient for this, though.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions