Reorder rows in data.table in a specific order

前端 未结 2 906
野趣味
野趣味 2021-02-07 00:05

I have a data.table dumdt:

set.seed(123)
dumdt <- data.table(v1=sample(1:10, 5), v2=1:5)

whose rows I\'d like to r

2条回答
  •  难免孤独
    2021-02-07 00:48

    If I understand correctly, you can just add a col and then order by it:

    setorder(dumdt[, .r := order(to_ord)], .r)[, .r := NULL]
    
       v1 v2
    1:  4  3
    2:  6  5
    3:  8  2
    4:  3  1
    5:  7  4
    

提交回复
热议问题