-5

Possible Duplicate:
How to sort a dataframe by column(s) in R

everyone: I want to sort the data within groups using R. For example: the data is:

geneID  Nr.annotation
CL1002  uncharacterized
CL1002  uncharacterized
CL1002  uncharacterized
CL1002  uncharacterized
CL1002  uncharacterized
CL1002  uncharacterized
CL1019  PREDICTED:
CL1019  PREDICTED:
CL1019  predicted
CL1019  PREDICTED:
CL1019  PREDICTED:
CL1019  predicted
CL1019  predicted
CL1019  hypothetical
CL10246 unnamed
CL10246 predicted
CL10246 unnamed
CL10246 unnamed
CL10246 unnamed
CL10246 unnamed
CL10246 unnamed
CL1064  octicosapeptide/Phox/Bem1p
CL1064  octicosapeptide/Phox/Bem1p
CL1064  serine/threonine
CL1064  serine/threonine
CL1064  serine/threonine
CL1064  serine/threonine

This is the original data. Please copy and use the code as follows:

read.table("clipboard",strip.white=T,header=T)

And then, what should I do? After sorted, I want the result to be this:

geneID  Nr.annotation
CL1002  PREDICTED:
CL1002  uncharacterized    4
CL1019  PREDICTED:         6
CL1019  hypothetical       1
CL1019  uncharacterized    1
CL10246 predicted          1
CL10246 unnamed            6
CL1064  octicosapeptide/Phox/Bem1p  2
CL1064  serine/threonine   4
Community
  • 1
  • 1
Sandy
  • 99
  • 1
  • 9

1 Answers1

1

You can use plyr and arrange. All you are doing is ordering first by one column, then the next

library(plyr)

arrange(DF,geneID, Nr.annotation)

(on second looking, your example output does not quite make sense. How are you getting a combination of

CL1002 PREDICTED:

mnel
  • 113,303
  • 27
  • 265
  • 254
  • arrange(DF,DF$geneID,DF$Nr,annotation)->z Error in order(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, : argument lengths differ – Sandy Nov 15 '12 at 03:14
  • Why have you included the `$` references? There is no need when using `arrange` (and my solution doesn't suggest it) – mnel Nov 15 '12 at 03:17
  • you are right. I made a mistake and you solution works. Thanks a lot. – Sandy Nov 15 '12 at 03:20