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