- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想根据字符变量(在我的示例中为 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/
我是一名优秀的程序员,十分优秀!