Vit*_*hKa 8 sorting r vector character
R中是否有内置功能可以对字符向量进行排序?sort并order忽略这种情况:
tv <- c("a", "A", "ab", "B")
sort(tv)
## [1] "a" "A" "ab" "B"
Run Code Online (Sandbox Code Playgroud)
到目前为止这是我的解决方案:
CAPS <- grep("^[A-Z]", tv)
c(sort(tv[CAPS]), sort(tv[-CAPS]))
## [1] "A" "B" "a" "ab"
Run Code Online (Sandbox Code Playgroud)
Mar*_*rek 12
继后约在记事本中自动完成++,你可以改变当地的设置:
Sys.setlocale(, "C")
sort(tv)
# [1] "A" "B" "a" "ab"
Run Code Online (Sandbox Code Playgroud)
编辑.我阅读了帮助页面,Sys.setlocale似乎改变LC_COLLATE就足够了:Sys.setlocale("LC_COLLATE", "C")
要临时更改整理以进行排序,您可以使用withr包:
withr::with_collate("C", sort(tv))
Run Code Online (Sandbox Code Playgroud)
或使用stringr包(如在@dracodoc评论中):
stringr::str_sort(tv, locale="C")
Run Code Online (Sandbox Code Playgroud)
我认为这是最好的方法.
| 归档时间: |
|
| 查看次数: |
835 次 |
| 最近记录: |