Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
知道如何按 ASCII 顺序(如 C)对包含非字母数字字符的字符向量进行排序吗?一个例子来说明:
> v<-c("#1-adfgh3$","-d","!cd3&") > sort(v) [1] "-d" "!cd3&" "#1-adfgh3$"
然而 ”!” 应该按 ASCII 顺序出现在“-”之前。
非常感谢。
干杯,约翰
这是一个想法:
asciiSort <- function(vec) { x <- sapply(vec, function(X) { paste0(strtoi(charToRaw(X), base=16), collapse="") }) vec[order(x)] } asciiSort(v) # [1] "!cd3&" "#1-adfgh3$" "-d"