提问人:rinzy kutex 提问时间:7/9/2013 最后编辑:Jaaprinzy kutex 更新时间:7/27/2022 访问量:56632
如何对包含字母和数字的元素向量进行排序?
How to sort a character vector where elements contain letters and numbers?
问:
我有一个字符数组
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
我想按降序对它进行排序,以便我得到这样的输出:
V51
V108
V116
V120
V155
V217
V327
V440
V446
V457
V477
我试过这样的sort.list()
cf[sort.list(cf)]
并得到这个答案:
[1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51"
并且也尝试并得到了相同的结果。order()
有人可以帮我吗
答:
79赞
A5C1D2H2I1M1N2O1R2T1
7/9/2013
#1
尝试从“gtools”包中尝试:mixedsort
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
如果你不想使用(不知道为什么不使用),并且如果你的向量有一个非常一致的模式(例如字母后跟数字),你也可以尝试这样的东西。(注:相对未经测试。mixedsort
)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
12赞
David Marx
7/9/2013
#2
只需刮掉前面的“V”字符即可构建排序向量。无需额外的花哨工具。
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
6赞
nico
7/9/2013
#3
R 正确地按字母顺序对字符串进行排序,这就是你得到这个结果的原因。
除了@Ananda非常好的答案之外,如果你想使用基本 R,你可以使用从每个字符串中删除“V”,然后用于将字符串转换为整数:strsplit
as.numeric
vals <- as.numeric(sapply(cf, FUN=function(x){strsplit(x, "V")[[1]][2]}))
现在,您可以使用以下命令对字符串进行排序vals
cf[order(vals)]
2赞
Tyler Rinker
7/9/2013
#4
这是一个利用 和 的基本方法(Anand's 非常巧妙):names
sort
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
63赞
Matthew Plourde
7/9/2013
#5
这里有很多正确的答案,这是另一种方式,只是为了好玩。
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
评论
2赞
dpelisek
5/11/2021
好吧,这会将 c(“ahoy”, “hello”, “hi”) 排序为 “hi”、“ahoy” 和 “hello”,这并不完全是人类对自然比较器的期望
57赞
JDie
7/29/2018
#6
使用函数的一行代码中的另一种解决方案(来自 packg。str_sort
stringr
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
上一个:在 R 中显示变量的精确值
评论