[R] sorting with criteria that are "out of order"

Wolfgang Huber huber at ebi.ac.uk
Mon Mar 19 18:10:35 CET 2007


Dear Niels,

you can convert the columns (which are apparently character vectors) 
into ordered factors and then sort / order on these.

 > a
  [1] "b" "a" "a" "a" "c" "a" "a" "b" "b" "c" "b" "a" "c" "c"
      "c" "c" "a" "a" "a" "c"

 > b=ordered(a, levels=c("b", "a", "c"))

 > b
  [1] b a a a c a a b b c b a c c c c a a a c
Levels: b < a < c

 > sort (b)
  [1] b b b b a a a a a a a a a c c c c c c c
Levels: b < a < c


Best wishes
   Wolfgang

------------------------------------------------------------------
Wolfgang Huber  EBI/EMBL  Cambridge UK  http://www.ebi.ac.uk/huber

  Steen Krogh wrote:
> I try to sort this dataframe: 
>      [,1]  [,2]     [,3]        [,4]  [,5]  [,6]  [,7]  [,8] 
>  [1,] "CM"  "BARBY"  "INCREASED" "  0" "  2" "  0" "  1" "  1"
>  [2,] "CM"  "BARBY"  "REDUCED"   "  0" "  1" "  2" "  2" "  5"
>  [3,] "CM"  "BARBY"  "STANDARD"  " 93" " 51" " 56" " 41" " 77"
>  [4,] "CM"  "BONBON" "INCREASED" " 43" " 30" " 39" " 32" " 58"
>  [5,] "CM"  "BONBON" "REDUCED"   "  4" "  3" "  6" "  4" " 10"
>  [6,] "CM"  "BONBON" "STANDARD"  "200" "141" "127" " 73" "134"
>  [7,] "RAR" "BARBY"  "INCREASED" "  4" "  1" "  3" "  1" "  5"
>  [8,] "RAR" "BARBY"  "REDUCED"   "  5" "  7" "  8" "  9" " 16"
>  [9,] "RAR" "BARBY"  "STANDARD"  "571" "286" "314" "270" "467"
> [10,] "RAR" "BONBON" "INCREASED" " 49" " 92" "108" "154" "240"
> [11,] "RAR" "BONBON" "REDUCED"   " 11" "  9" "  5" "  6" " 18"
> [12,] "RAR" "BONBON" "STANDARD"  "978" "627" "571" "324" "541"
> 
> 
> I want the sorting criteria: 
> Column 1: CM before RAR
> Column 2: BONBON before BARBY
> Column 3: REDUCED before STANDARD before INCREASED
> 
> Have played with the "order" function but without being able to "sort out" how
> to sort using information in column three. 
> 
> /Niels 
> 
> Niels Steen Krogh
> Konsulent
> ZiteLab ApS 
> 
> Mail: ---------- nielssteenkrogh a zitelab.dk
> Telefon: ------- +45 38 88 86 13
> Mobil: --------- +45 22 67 37 38
> Adresse: ------- ZiteLab ApS 
> ---------------- Solsortvej 44
> ---------------- 2000 F.
> Web: ----------- www.zitelab.dk
> 
> ZiteLab
> -Let's Empower Your Data with Webservices
>



More information about the R-help mailing list