-
Notifications
You must be signed in to change notification settings - Fork 1k
Open
Labels
Milestone
Description
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.
jangorecki