2009/03/19

mixed type sort

It's a common problem in semiconductor EDA analysis which plot by lot+wafer number as axis ticks. But the usual sort cannot get the right order for character embed numbers.
The package gtools has the "mixedsort" method for solving this problem.
>library(gtools)
>x <- paste("AA_",1:24, sep="")
>(y <- sort(x))
[1] "AA_1" "AA_10" "AA_11" "AA_12" "AA_13" "AA_14" "AA_15" "AA_16" "AA_17" "AA_18" AA_19"
[12] "AA_2" "AA_20" "AA_21" "AA_22" "AA_23" "AA_24" "AA_3" "AA_4" "AA_5" "AA_6" "AA_7"
[23] "AA_8" "AA_9"
>(y1 <- mixedsort(x))
[1] "AA_1" "AA_2" "AA_3" "AA_4" "AA_5" "AA_6" "AA_7" "AA_8" "AA_9" "AA_10" "AA_11"
[12]"AA_12" "AA_13" "AA_14" "AA_15" "AA_16" "AA_17" "AA_18" "AA_19" "AA_20" "AA_21" "AA_22"
[23]"AA_23" "AA_24"
#you can use mixedorder to get the order
> (y1 <- order(x))
[1] 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 3 4 5 6 7 8 9
> (y2 <- mixedorder(x))
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
view raw mixedsort hosted with ❤ by GitHub

沒有留言:

CC Copyright

創用 CC 授權條款
本著作由Chunhung Chou製作,以創用CC 姓名標示-相同方式分享 3.0 Unported 授權條款釋出。