我有以下data.frame"test":
Cytoband
9p
1q
10p
22p
2q
Run Code Online (Sandbox Code Playgroud)
我想得到:
Cytoband
1q
2q
9p
10p
22p
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
indices <- order(test$Cytoband, decreasing = FALSE)
test <- test[indices,]
Run Code Online (Sandbox Code Playgroud)
但我得到:
10p
1q
22p
2q
9p
Run Code Online (Sandbox Code Playgroud)
有简单的修改吗?谢谢!
x = c("9p","1q","10p","22p","2q")
y = x[order(as.numeric(gsub("\\D","",x)))]
y
[1] "1q" "2q" "9p" "10p" "22p"
Run Code Online (Sandbox Code Playgroud)