[R] How to sort using a predefined criterion

From: Vincy Pyne <vincy_pyne_at_yahoo.ca>
Date: Tue, 08 Mar 2011 00:33:49 -0800 (PST)


Dear R helpers,

Suppose I have following data.frame.

df <- data.frame(category = c("treat_A", "treat_A", "treat_A", "treat_A", "treat_A", "treat_A", "treat_A", "treat_A", "treat_B", "treat_B", "treat_B", "treat_B", "treat_B", "treat_B", "treat_B", "treat_B"), type = c("AA", "AAAA", "B", "AAA", "BB", "BBBB", "BBB", "AAA", "B", "AAA", "BBB", "AA", "AAAA", "BB", "A", "BBBB"), values = c(0.382000183, 0.100680563, 0.596484268, 0.899105808, 0.884609516, 0.958464309, 0.014496292, 0.407422102, 0.863246559, 0.138584552, 0.245033113, 0.045472579, 0.032380139, 0.164128544, 0.219611194, 0.017090365))

> df

   category     type          values
1   treat_A     
 AA       0.38200018

2   treat_A    AAAA    0.10068056
3   treat_A       B        0.59648427
4   treat_A     AAA     0.89910581
5   treat_A     BB       0.88460952
6   treat_A   BBBB    0.95846431
7   treat_A    BBB     0.01449629
8   treat_A       A      0.40742210
9   treat_B      B       0.86324656
10  treat_B   AAA     0.13858455

11  treat_B   BBB     0.24503311
12  treat_B   
 AA      0.04547258
13  treat_B  AAAA   0.03238014
14  treat_B    BB      0.16412854
15  treat_B     A       0.21961119
16  treat_B  BBBB    0.01709036

I need to sort above dataframe for the category treat_A and treat_B type-wise i.e. in the order (AAAA, AAA, AA, A, BBBB, BBB, BB, B) Thus I need

   category     type         values
1   treat_A     AAAA      0.10068056
2   treat_A      AAA       0.89910581
3   treat_A       AA        0.38200018
4  
 treat_A        A         0.40742210
5   treat_A     BBBB     0.95846431
6   treat_A      BBB      0.01449629
7   treat_A       BB       0.88460952
8   treat_A        B        0.59648427
9   treat_B     AAAA    0.03238014
10  treat_B     AAA      0.13858455
11  treat_B      AA       0.04547258

12  treat_B       A        0.21961119
13  treat_B    
 BBBB   0.01709036
14  treat_B      BBB    0.24503311
15  treat_B       BB     0.16412854
16  treat_B        B      0.86324656

Kindly advice how this can be achieved. I referred to "?sort" and "?order" literature, but couldn't find any example of this sort.

Thanking you in advance.

Vincy

        [[alternative HTML version deleted]]



R-help_at_r-project.org 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. Received on Tue 08 Mar 2011 - 08:39:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Mar 2011 - 10:10:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive