R:gtools混合排序的意外自然排序

标签 r sorting vector natural-sort

我只是在 gtools::mixedsort 中发现了一些意想不到的行为看看我的输出是如何自然排序的。
我有一个这样的例子:

aa=c("CD57","CD58","CD158","CD158b","CD158e","CD158e1","CD319","CD335")
gtools::mixedsort(aa)
我的预期结果是:
[1] "CD57"    "CD58"    "CD158"   "CD158b"  "CD158e"  "CD158e1" "CD319"
[8] "CD335"
但是我得到了这个:
[1] "CD57"    "CD58"    "CD158"   "CD158b"  "CD158e"  "CD319"   "CD335"
[8] "CD158e1"
这样对吗?是什么原因?

最佳答案

CD158e1被视为 1580在这里,因为:

>>> 158e1
1580.0
>>> 
158e1是一个带有 e 的欧拉数,所以它多给一个 0 , e2会给2个额外的等等......
所以这就是为什么它被解析为列表中的最后一个。

the documentation of mixedsort 中所述:

These functions sort or order character strings containing embedded numbers so that the numbers are numerically sorted rather than sorted by character value. I.e. "Aspirin 50mg" will come before "Aspirin 100mg". In addition, case of character strings is ignored so that "a", will come before "B" and "C".

关于R:gtools混合排序的意外自然排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69388929/

相关文章:

r - ggplot2 的 "parent element"是什么?

c - 从源代码构建 R 包 krb5

java - 如何对包含由 '|~' 分隔的多个字段的 List<String> 进行排序

c++ - 使用索引避免迭代器失效,保持干净的界面

c++ - 关于 vector 还有其他需要了解的事情吗

r - 在 R/Rmarkdown 中同步两个传单 map

r - 使用 purrr :map to loop through web pages for scraping with Rselenium

algorithm - Scala:如何通过分组(或分箱)获取 Iterable 的前 N ​​个元素

c - 使用指针而不是索引进行排序

c++ - 检查两个 vector 是否相等