我有一个字符数组

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()并得到了相同的结果。

有人可以帮我吗?

#1 楼

这里有很多正确的答案,这是另一种方式,只是为了好玩。

cf[order(nchar(cf), cf)]
# [1] "V51"  "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"


#2 楼

请尝试从“ 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" 


#3 楼

使用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"


#4 楼

只需刮掉前面的“ V”字符即可构建排序向量。不需要其他高级工具。

vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]

[1] "V51"  "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"


#5 楼

R正确地按字母顺序对字符串排序,这就是为什么要得到该结果的原因。
除了@Ananda很好的答案,如果您想使用基数R,则可以使用strsplit从每个字符串中删除“ V”然后使用as.numeric将字符串转换为整数:

vals <- as.numeric(sapply(cf, FUN=function(x){strsplit(x, "V")[[1]][2]}))


现在您可以使用vals

cf[order(vals)]

对字符串进行排序

#6 楼

这是利用namessort(Ananda相当漂亮)的基本方法:

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"