我要订购以下数据
col.to_order <- paste(rep(c("a",'b','c'),times=100), 1:100,sep = "")
[1] "a1" "b2" "c3" "a4" "b5" "c6" "a7" "b8" "c9" "a10" "b11" "c12" "a13" "b14" "c15" "a16" "b17" "c18"
[19] "a19" "b20" "c21" "a22" "b23" "c24" "a25" "b26" "c27" "a28" "b29" "c30" "a31" "b32" "c33" "a34" "b35" "c36"
[37] "a37" "b38" "c39" "a40" "b41" "c42" "a43" "b44" "c45" "a46" "b47" "c48" "a49" "b50" "c51" "a52" "b53" "c54"
[55] "a55" "b56" "c57" "a58" "b59" "c60" "a61" "b62" "c63" "a64" "b65" "c66" "a67" "b68" "c69" "a70" "b71" "c72"
[73] "a73" "b74" "c75" "a76" "b77" "c78" "a79" "b80" "c81" "a82" "b83" "c84" "a85" "b86" "c87" "a88" "b89" "c90"
我想通过以下方式订购数据
"a1" "b1" "c1" "a2" "b2" "c2" "a3" "b3" "c3" ... "a100" "b100" "c100".
我该怎么做?
答案 0 :(得分:2)
当您将其称为列时,我认为您具有df。然后,dplyr
的可能性可能是:
df %>%
arrange(as.numeric(gsub("\\D", "", col.to_order)))
col.to_order
1 a1
2 b1
3 c1
4 b2
5 c2
6 a2
7 c3
8 a3
9 b3
10 a4
使用base R
(这只是一个向量):
col.to_order[order(as.numeric(gsub("\\D", "", col.to_order)))]
[1] "a1" "b1" "c1" "b2" "c2" "a2" "c3" "a3" "b3" "a4" "b4"
答案 1 :(得分:1)
使用tidyverse
的选项是使用parse_number
提取数字部分
library(readr)
library(dplyr)
df %>%
arrange(parse_number(col.to_order))
# col.to_order
#1 a1
#2 b1
#3 c1
#4 b2
#5 c2
#6 a2
#7 c3
#8 a3
#9 b3
#10 a4
#...
df <- data.frame(col.to_order, stringsAsFactors = FALSE)