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

时间:2012-11-15 02:54:43

标签: r sorting

  

可能重复:
  How to sort a dataframe by column(s) in R

大家:     我想使用R对组内的数据进行排序。例如: 数据是:

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

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用plyrarrange。您所做的只是首先按一列排序,然后是下一个

library(plyr)

arrange(DF,geneID, Nr.annotation)

(第二眼看,你的示例输出没有多大意义。你是如何得到

的组合

CL1002 PREDICTED: