关于排序:如何对组内的数据进行排序并计算R中每个组的元素?

How to sort data within groups and count the elements of each group in R?

本问题已经有最佳答案,请猛点这里访问。

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

所有人:我想使用r对组内的数据进行排序。例如:数据是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

这是原始数据。请复制并使用以下代码:

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

然后,我该怎么做?排序后,我希望结果如下:

1
2
3
4
5
6
7
8
9
10
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

您可以使用plyrarrange。你要做的就是先按一列排序,然后按下一列排序

1
2
3
library(plyr)

arrange(DF,geneID, Nr.annotation)

(第二步,您的示例输出不太有意义。你怎么得到一个组合

埃多克斯1〔2〕