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 at 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
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
>
What you need to do is to create ordered factors from the data in the order that you want them sorted:> x <- '"CM" "BARBY" "INCREASED" " 0" " 2" " 0" " 1" " 1"+ "CM" "BARBY" "REDUCED" " 0" " 1" " 2" " 2" " 5" + "CM" "BARBY" "STANDARD" " 93" " 51" " 56" " 41" " 77" + "CM" "BONBON" "INCREASED" " 43" " 30" " 39" " 32" " 58" + "CM" "BONBON" "REDUCED" " 4" " 3" " 6" " 4" " 10" + "CM" "BONBON" "STANDARD" "200" "141" "127" " 73" "134" + "RAR" "BARBY" "INCREASED" " 4" " 1" " 3" " 1" " 5" + "RAR" "BARBY" "REDUCED" " 5" " 7" " 8" " 9" " 16" + "RAR" "BARBY" "STANDARD" "571" "286" "314" "270" "467" + "RAR" "BONBON" "INCREASED" " 49" " 92" "108" "154" "240" + "RAR" "BONBON" "REDUCED" " 11" " 9" " 5" " 6" " 18" + "RAR" "BONBON" "STANDARD" "978" "627" "571" "324" "541" + '> x.in <- read.table(textConnection(x)) # read in the data frame > x.in$V1 <- factor(x.in$V1, levels=c("CM", "RAR")) # create orderedfactors> x.in$V2 <- factor(x.in$V2, levels=c("BONBON", "BARBY")) > x.in$V3 <- factor(x.in$V3, levels=c("REDUCED", "STANDARD", "INCREASED")) > # sort by all three columns > x.in[order(x.in$V1, x.in$V2, x.in$V3),]V1 V2 V3 V4 V5 V6 V7 V8 5 CM BONBON REDUCED 4 3 6 4 10 6 CM BONBON STANDARD 200 141 127 73 134 4 CM BONBON INCREASED 43 30 39 32 58 2 CM BARBY REDUCED 0 1 2 2 5 3 CM BARBY STANDARD 93 51 56 41 77 1 CM BARBY INCREASED 0 2 0 1 1 11 RAR BONBON REDUCED 11 9 5 6 18 12 RAR BONBON STANDARD 978 627 571 324 541 10 RAR BONBON INCREASED 49 92 108 154 240 8 RAR BARBY REDUCED 5 7 8 9 16 9 RAR BARBY STANDARD 571 286 314 270 467 7 RAR BARBY INCREASED 4 1 3 1 5 On 3/19/07, Niels Steen Krogh <nielssteenkrogh@zitelab.dk> 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@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 > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]