-
Notifications
You must be signed in to change notification settings - Fork 1k
Infer cols from named order in setorderv() (#6932) #7861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21891,3 +21891,10 @@ DF = DF[, 'a', drop=FALSE] | |
| test(2382.08, subset(DT, a > 5, select="a", drop=TRUE), subset(DF, a > 5, select="a", drop=TRUE)) | ||
| test(2382.09, subset(DT, a > 10, select="a", drop=TRUE), subset(DF, a > 10, select="a", drop=TRUE)) | ||
| test(2382.10, subset(DT, a > 5, drop=TRUE), subset(DF, a > 5, drop=TRUE)) | ||
|
|
||
| # #6392 setorderv() could take order=<column-order mapping> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldnt this be 6932? |
||
| test(2383.01, {DT1=copy(data.table(a=c(2,1,2),b=c("b","a","b"),c=c(5,10,3))); DT2=copy(DT1); m=c(a=1L,b=1L,c=-1L); setorderv(DT1, order=m); setorderv(DT2, cols=names(m), order=m); identical(DT1, DT2)}, TRUE) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the extra copy for DT1? please use a setup please. Please supply |
||
| test(2383.02, {DT=data.table(x=c(2,1,2),y=c(2,1,1)); setorderv(DT, order=c(x=1L,y=-1L)); DT}, data.table(x=c(1,2,2),y=c(1,2,1))) | ||
| test(2383.03, setorderv(data.table(x=1:3), order=c(y=1L)), error="some columns are not in the data.table") | ||
| test(2383.04, setorderv(data.table(x=1:3), order=c(x=1L,x=-1L)), error="order argument has named duplicates") | ||
| test(2383.05, setorderv(data.table(x=1:3), order=c(x=2L)), error="Must be +1 or -1") | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,11 +42,13 @@ is missing (ex: \code{setorder(x)}), \code{x} is rearranged based on all | |||||
| columns in ascending order by default. To sort by a column in descending order | ||||||
| prefix the symbol \code{"-"} which means "descending" (\emph{not} "negative", in this context), i.e., \code{setorder(x, a, -b, c)}. The \code{-b} works | ||||||
| when \code{b} is of type \code{character} as well. } | ||||||
| \item{cols}{ A character vector of column names of \code{x} by which to order. By default, sorts over all columns; \code{cols = NULL} will return \code{x} untouched. Do not add \code{"-"} here. Use \code{order} argument instead. } | ||||||
| \item{cols}{ A character vector of column names of \code{x} by which to order. By default, sorts over all columns; \code{cols = NULL} will return \code{x} untouched. Do not add \code{"-"} here. Use \code{order} argument instead. This argument can be omitted if \code{order} is a named vector. } | ||||||
| \item{order}{ An integer vector with only possible values of \code{1} and | ||||||
| \code{-1}, corresponding to ascending and descending order. The length of | ||||||
| \code{order} must be either \code{1} or equal to that of \code{cols}. If | ||||||
| \code{length(order) == 1}, it is recycled to \code{length(cols)}. } | ||||||
| \code{length(order) == 1}, it is recycled to \code{length(cols)}. \code{order} | ||||||
| can also be a named vector, in which case the names are used as \code{cols} | ||||||
| and the \code{cols} argument can be omitted. } | ||||||
| \item{na.last}{ \code{logical}. If \code{TRUE}, missing values in the data are placed last; if \code{FALSE}, they are placed first; if \code{NA} they are removed. | ||||||
| \code{na.last=NA} is valid only for \code{x[order(., na.last)]} and related \code{sort_by(x, .)} (\R \ifelse{html}{\out{≥}}{\eqn{\ge}} 4.4.0) and its | ||||||
| default is \code{TRUE}. \code{setorder} and \code{setorderv} only accept | ||||||
|
|
@@ -65,7 +67,9 @@ Note that \code{-b} also works with columns of type \code{character} unlike | |||||
| \code{\link[base]{order}}, which requires \code{-xtfrm(y)} instead (which is slow). | ||||||
|
|
||||||
| \code{setorderv} in turn accepts a character vector of column names and an | ||||||
| integer vector of column order separately. | ||||||
| integer vector of column order separately. Additionally, \code{setorderv} can | ||||||
| accept a named integer vector for \code{order}, in which case \code{cols} is | ||||||
| inferred from the names and can be omitted. | ||||||
|
|
||||||
| Note that \code{\link{setkey}} still requires and will always sort only in | ||||||
| ascending order, and is different from \code{setorder} in that it additionally | ||||||
|
|
@@ -133,6 +137,10 @@ setorder(DT, A, -B) | |||||
|
|
||||||
| # same as above, but using setorderv | ||||||
| setorderv(DT, c("A", "B"), c(1, -1)) | ||||||
|
|
||||||
| # infer cols from named order mapping (v1.18.99+) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
please dont put versions here. First and foremost they will be outdated. |
||||||
| mapping = c(A = 1L, B = -1L) | ||||||
| setorderv(DT, order = mapping) | ||||||
| } | ||||||
| \keyword{ data } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NEWS item is unfinished