R 中 gtools::mixedsort 包的奇怪行为

Strange behavior with gtools::mixedsort package in R

我喜欢使用 gtools 包中的 mixedsort/order 函数进行字母数字排序。然而,我遇到了一个非常奇怪的行为,在某些情况下它不能很好地与角色 E 一起工作。查看一些例子。有人能帮我吗 ?怎么了?

正确排序:

> mixedsort(c("P0A1","P0B1","P0C1","P0D1","P0G1","P0F1"))
[1] "P0A1" "P0B1" "P0C1" "P0D1" "P0F1" "P0G1"

字符 E 的排序不正确:

> mixedsort(c("P0A1","P0E1","P0C1","P0D1","P0G1","P0F1"))
[1] "P0E1" "P0A1" "P0C1" "P0D1" "P0F1" "P0G1"

正确排序字符 E:

> mixedsort(c("PA1","PE1","PC1","PD1","PG1","PF1"))
[1] "PA1" "PC1" "PD1" "PE1" "PF1" "PG1"

在R中0E1是浮点数0:

0E1
## [1] 0

看来你需要给它一些帮助。在最后一个非数字后插入一个space,排序后去掉

sub(" ", "", mixedsort(sub("^(.*\D)", "\1 ", x)))
## [1] "P0A1" "P0C1" "P0D1" "P0E1" "P0F1" "P0G1"