Reputation: 1453
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.
Upvotes: 9
Views: 4134
Reputation: 38500
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.
Upvotes: 12