我有以下data.frame“test”:
Cytoband
9p
1q
10p
22p
2q
我想得到:
Cytoband
1q
2q
9p
10p
22p
这是我的代码:
indices <- order(test$Cytoband, decreasing = FALSE)
test <- test[indices,]
但我明白了:
10p
1q
22p
2q
9p
有简单的修改吗?谢谢!
答案 0 :(得分:5)
x = c("9p","1q","10p","22p","2q")
y = x[order(as.numeric(gsub("\\D","",x)))]
y
[1] "1q" "2q" "9p" "10p" "22p"
答案 1 :(得分:2)
您也可以尝试使用mixedsort
或mixedorder
library(gtools)
out = data.frame(Cytoband = mixedsort(dat$Cytoband))
#> out
# Cytoband
#1 1q
#2 2q
#3 9p
#4 10p
#5 22p
使用mixedorder
dat[mixedorder(as.character(dat$Cytoband)),]
答案 2 :(得分:1)
test[order(as.numeric(gsub('(\\d+)[a-z]*', '\\1', test[,1]))),, drop=F]
Cytoband
2 1q
5 2q
1 9p
3 10p
4 22p