Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a data.table programmatically using character vector of multiple column names

I need to sort a data.table on multiple columns provided as character vector of variable names.

This is my approach so far:

DT = data.table(x = rep(c("b","a","c"), each = 3), y = c(1,3,6), v = 1:9)

#column names to sort by, stored in a vector
keycol <- c("x", "y")

DT[order(keycol)]
   x y v
1: b 1 1
2: b 3 2

Somehow It displays just 2 rows and removes other records. But if I do this:

DT[order(x, y)]
   x y v
1: a 1 4
2: a 3 5
3: a 6 6
4: b 1 1
5: b 3 2
6: b 6 3
7: c 1 7
8: c 3 8
9: c 6 9

It works like fluid. Can anyone help with sorting using column name vector?

like image 298
RajnishDwivedy Avatar asked May 10 '18 22:05

RajnishDwivedy


People also ask

How do you sort data by multiple fields?

Right-click a field name and click Advanced Sorting. Click the Sort by arrow and click the first field by which you want to sort. Click Ascending or Descending to choose the sort order. Click the Then sort by arrow, click the next field, then choose a sort order.

How do I sort multiple columns in R?

Sort Rows by Multiple Columns using arrange() In R, the arrange() can also be used to sort the dataframe by multiple columns in ascending or descending order, By default it will sort in ascending order. It will take the dataframe name as the first parameter and column names as the next parameter.

Can you group by multiple columns in R?

Grouping can be also done using multiple columns belonging to the data frame for this just the names of the columns have to be passed to the function.

How do you sort a Dataframe according to a column in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order. Here are some examples.


1 Answers

You need ?setorderv and its cols argument:

A character vector of column names of x by which to order

library(data.table)
DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
#column vector
keycol <-c("x","y")
setorderv(DT, keycol)
DT
   x y v
1: a 1 4
2: a 3 5
3: a 6 6
4: b 1 1
5: b 3 2
6: b 6 3
7: c 1 7
8: c 3 8
9: c 6 9

Note that there is no need to assign the output of setorderv back to DT. The function updates DT by reference.

like image 89
Ameya Avatar answered Sep 19 '22 11:09

Ameya