gpt4 book ai didi

r - order data.table along numeric column puttint special Value (residual category) of other column 最后

转载 作者:行者123 更新时间:2023-12-04 10:27:44 26 4
gpt4 key购买 nike

是否有可能沿着数字列对 data.table 进行排序,同时将特殊行(剩余类别)放在最后(基于另一列的标签)?

DT <- data.table (a =c("a", "sd", "set", "c", "s", "REST", "sde", "de"), 
b=c(5,1:4, 2.5, 1:2))

预期输出类似于

      a   b
1: sd 1.0
2: sde 1.0
3: set 2.0
4: de 2.0
5: c 3.0
6: s 4.0
7: a 5.00
8: REST 2.5

我在下面给出了一个答案,但我想知道是否有一种没有 rbind 的可能性。这很复杂,我想这也会复制整个 data.table ;)

最佳答案

您可以将表达式传递给 order

DT[order(a=="REST", b)] # internally optimised to use data.table's fast ordering

如果您想改用 setorder(通过引用重新排序):

DT[, tmp := a == "REST"]
setorder(DT, tmp, b)

关于r - order data.table along numeric column puttint special Value (residual category) of other column 最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com