You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Note that ?base::is.unsorted contains "This function is designed for objects with one-dimensional indices, as described above. Data frames, matrices and other arrays may give surprising results.". IIRC, it pastes together the character representation of the rows and then compares those strings. Accordingly, a huge number of long strings are created and hence is very slow. I'm not sure what the surprising cases are but I think it might involve at least user options like scipen.
In contrast, data.table::is.sorted has a columnar algorithm to do it, and is aware of integer64 too for example. It's much faster for data.frame|table (10x + ?) and actually correct.
For straightforward vectors they're pretty much the same. After all, it's just iterating if < when a single vector (there is no sorting algorithm needed).
Agree with exporting. Just need some documentation. My comments here could be basically it.
(It's also fairly easily to parallize. Just needs to ensure each chunk overlaps the previous one by 1-row, and return as soon as any chunk finds unsorted.)
Is it safe to assume that is.sorted(list()) or is.sorted(data.table()) to return TRUE?
And what about is.sorted(NULL)? Returning NA for NULL doesn't seem to be bad idea IMO.
Activity
MichaelChirico commentedon Sep 6, 2017
Doesn't
is.unsorteduse the same sorting algorithm thatdata.tabledoes now?https://stackoverflow.com/q/31774807/3576984
mattdowle commentedon May 23, 2019
@MichaelChirico It's very different.
Note that
?base::is.unsortedcontains "This function is designed for objects with one-dimensional indices, as described above. Data frames, matrices and other arrays may give surprising results.". IIRC, it pastes together the character representation of the rows and then compares those strings. Accordingly, a huge number of long strings are created and hence is very slow. I'm not sure what the surprising cases are but I think it might involve at least user options like scipen.In contrast,
data.table::is.sortedhas a columnar algorithm to do it, and is aware ofinteger64too for example. It's much faster fordata.frame|table(10x + ?) and actually correct.For straightforward vectors they're pretty much the same. After all, it's just iterating
if <when a single vector (there is no sorting algorithm needed).Agree with exporting. Just need some documentation. My comments here could be basically it.
(It's also fairly easily to parallize. Just needs to ensure each chunk overlaps the previous one by 1-row, and return as soon as any chunk finds unsorted.)
[-][Request] export is.sorted[/-][+]export is.sorted[/+]jangorecki commentedon Apr 11, 2020
Is it safe to assume that
is.sorted(list())oris.sorted(data.table())to return TRUE?And what about
is.sorted(NULL)? Returning NA for NULL doesn't seem to be bad idea IMO.