Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order a data.table based on a character column with a specific (not alphabetical) order in mind

Tags:

r

data.table

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.

like image 914
Jakob Avatar asked Mar 02 '17 18:03

Jakob


People also ask

What is the Order of the data in the table?

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.

What is the order by criteria in MySQL?

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.

How to order data by alphabetical order in SQL?

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 ...

When do we use order by function in SQL?

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.


1 Answers

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.

like image 140
lmo Avatar answered Sep 20 '22 22:09

lmo