DT ...">

How to sort a data table using a target vector

So I have the following data.table

DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,2,3)) > DT xy 1: b 1 2: b 2 3: b 3 4: a 1 5: a 2 6: a 3 7: c 1 8: c 2 9: c 3 

And I have the following vector

 k <- c("2","3","1") 

I want to use k as the target vector to sort DT with y and get something like this.

 > DT xy 1: b 2 2: a 2 3: c 2 4: b 3 5: a 3 6: c 3 7: b 1 8: a 1 9: c 1 

Any ideas? If I use DT[order(k)] , I get a subset of the source data, and that is not what I am looking for.

+5
source share
2 answers

Call the match() call there.

 DT[order(match(y, as.numeric(k)))] # xy # 1: b 2 # 2: a 2 # 3: c 2 # 4: b 3 # 5: a 3 # 6: c 3 # 7: b 1 # 8: a 1 # 9: c 1 

In fact, DT[order(match(y, k))] will work, but it is probably safest to make match() arguments of the same class just in case.

Note: match() is known to be suboptimal in some cases. If you have a large number of lines, you can switch to fastmatch::fmatch for faster matching.

+7
source

You can do it:

 DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,2,3)) k <- c("2","3","1") setkey(DT,y) DT[data.table(as.numeric(k))] 

or (from Richard's comment)

 DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,2,3)) k <- c("2","3","1") DT[data.table(y = as.numeric(k)), on = "y"] 
+2
source

Source: https://habr.com/ru/post/1240189/


All Articles