gpt4 book ai didi

r - 字符变量的 order() 如何在 data.table 中工作?

转载 作者:行者123 更新时间:2023-12-04 08:22:30 24 4
gpt4 key购买 nike

我想根据字符变量(在我的示例中为 stage)对表中的行重新排序。如果我首先将所需的顺序( order(dt1$stage) )保存到一个变量中,然后将其应用为 dt1[myorder, stage] - 它工作正常。但是当我尝试执行相同的内联操作时,例如 dt1[order(dt1$stage), ] ,顺序不一样!一定是我错过的非常基本的东西......

dt1 <- fread('
id stage pos
1 I 1
2 II 2
3 III 5
4 IV 6
5 IIa 3
6 IIb 7
7 IIIa 8
8 IIIb 4
9 IVa 9
10 IVb 10')

sort(dt1$stage) # OK
# I II IIa IIb III IIIa IIIb IV IVa IVb

myorder <- order(dt1$stage)
dt1[myorder , stage] # OK
# I II IIa IIb III IIIa IIIb IV IVa IVb

dt1[order(dt1$stage), stage] # different!
# I II III IIIa IIIb IIa IIb IV IVa IVb

最佳答案

它正在做一个 fast order而不是 base::order .根据 ?data.table::order

Note that queries like x[order(.)] are optimised internally to use data.table's fast order.


Also note that data.table always reorders in "C-locale" (see Details). To sort by session locale, use x[base::order(.)].


data.table implements its own fast radix-based ordering.


data.table always reorders in "C-locale". As a consequence, the ordering may be different to that obtained by base::order. In English locales, for example, sorting is case-sensitive in C-locale. Thus, sorting c("c", "a", "B") returns c("B", "a", "c") in data.table but c("a", "B", "c") in base::ord


如果我们想复制 sort来自 base ,使用 base::order
dt1[base::order(stage)]$stage
#[1] "I" "II" "IIa" "IIb" "III" "IIIa" "IIIb" "IV" "IVa" "IVb"

关于r - 字符变量的 order() 如何在 data.table 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65432097/

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