简体   繁体   English

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

[英]How does order() for character variable work in data.table?

I want to reorder rows in my table according to a character variable ( stage in my example).我想根据字符变量(在我的示例中为stage )对表中的行进行重新排序。 If I first save the desired order ( order(dt1$stage) ) to a variable and then apply it as dt1[myorder, stage] - it works fine.如果我首先将所需的订单( order(dt1$stage) )保存到变量中,然后将其应用为dt1[myorder, stage] - 它工作正常。 But when I try to do the same inline, like dt1[order(dt1$stage), ] , the order is different.但是当我尝试做同样的内联时,比如dt1[order(dt1$stage), ] ,顺序是不同的。 Must be something very basic I'm missing...一定是我想念的非常基本的东西......

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

It is doing a fast order instead of the base::order .它正在fast order而不是base::order According to ?data.table::order根据?data.table::order

Note that queries like x[order(.)] are optimised internally to use data.table's fast order.请注意,像 x[order(.)] 这样的查询在内部进行了优化,以使用 data.table 的快速顺序。

Also note that data.table always reorders in "C-locale" (see Details).另请注意,data.table 始终在“C 语言环境”中重新排序(请参阅详细信息)。 To sort by session locale, use x[base::order(.)].要按 session 区域设置排序,请使用 x[base::order(.)]。

data.table implements its own fast radix-based ordering. data.table 实现了自己的基于基数的快速排序。

data.table always reorders in "C-locale". data.table 总是在“C 语言环境”中重新排序。 As a consequence, the ordering may be different to that obtained by base::order.因此,排序可能与通过 base::order 获得的不同。 In English locales, for example, sorting is case-sensitive in C-locale.例如,在英语语言环境中,排序在 C 语言环境中区分大小写。 Thus, sorting c("c", "a", "B") returns c("B", "a", "c") in data.table but c("a", "B", "c") in base::ord因此,排序 c("c", "a", "B") 在 data.table 中返回 c("B", "a", "c") 但 c("a", "B", "c") 在基础::ord

If we want to replicate the sort from base , use the base::order如果我们想从base复制sort ,请使用base::order

dt1[base::order(stage)]$stage
#[1] "I"    "II"   "IIa"  "IIb"  "III"  "IIIa" "IIIb" "IV"   "IVa"  "IVb" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM