Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (45.3k points)
edited by
How to sort a data.table using vector of multiple columns in R?

1 Answer

0 votes
by (41.4k points)

Here,you can use setorderv:

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

As the function updates DT by reference there is no need of assigning the output of setorderv back to DT. 

If you want to learn about R Programming then visit this R Programming Course.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...