Reputation: 4635
I have following data that I want to order
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"
I want to order data in following way
"a1" "b1" "c1" "a2" "b2" "c2" "a3" "b3" "c3" ... "a100" "b100" "c100".
How can I do that ?
Upvotes: 1
Views: 55
Reputation: 886938
An option with tidyverse
would be to extract the numeric part with 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)
Upvotes: 1
Reputation: 39858
As you call it a column, I consider you have a df. Then, a dplyr
possibility could be:
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
With base R
(this considers just a vector):
col.to_order[order(as.numeric(gsub("\\D", "", col.to_order)))]
[1] "a1" "b1" "c1" "b2" "c2" "a2" "c3" "a3" "b3" "a4" "b4"
Upvotes: 2