I want to order a data table based on a character vector specified manually.
library(data.table)
DT = data.table(x=c("c","b","a"), y=1:3)
I can order it alphabetically with:
DT[order(x)]
but can I order it based on a character vector like:
preferred.order <- c("b","a","c")
with the goal being:
data.table(x=c("b","a","c"), y=c(2,1,3))
In reality, I have a data.table with collected outputs and the variable names in the first column. For presentation purposes, I want those variables in a specific (not alphabetical) order.
The data can be ordered either in ascending or descending way. When columns are in string format or varchar datatype then order by results in sorting of data in alphabetical order when done in ascending manner.
It returns the sorted and ordered data based on a certain column (s) as the criteria to ORDER the data. The data can be ordered either in ascending or descending way. When columns are in string format or varchar datatype then order by results in sorting of data in alphabetical order when done in ascending manner.
SQL Order by Alphabetical can be done on character-based column values using simply ORDER BY clause in ascending order. In SQL, various clauses can be used with the SELECT clause to achieve some specific functionality or make the resultset to be retrieved in a particular format. ORDER BY clause is one such clause that helps in getting ...
Most of the time, the ORDER BY function is used when using the aggregate functions of SQL Whenever we do not specify the type of the order that is ascending or descending then by default the data is being ordered in ascending way.
One possibility is to join on the preferred order:
DT[preferred.order, on="x"]
x y
1: b 2
2: a 3
3: c 1
Note that this requires the preferred.order vector contains all elements in DT$x
and has no duplicates.
As an alternative, you could create a factor variable of DT$x
with the preferred ordering and then use setorder
to order DT by reference.
DT[, xFac := factor(x, levels=preferred.order)]
setorder(DT, xFac)
which returns
DT
x y xFac
1: b 2 b
2: a 3 a
3: c 1 c
Which method is preferable will vary on the use-case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With