我的问题很简单但有些我怎么也找不到如何排序数字。我希望它按组和等级排序(1,2,3,4,5,6,7,8,9, 10,11,12,13,14 )
means <- ddply(Data, ~Group ~rank, summarise, mean=mean(Foo))
#My column types
str(means)
#'data.frame': 56 obs. of 3 variables:
# $ Group: chr "dEC" "dEC" "dEC" "dEC" ...
# $ rank : chr "1" "10" "11" "12" ...
# $ mean : num 41.4 67.4 NA 65.9 71.3 ...
#means
Group rank mean
1 dEC 1 41.37500
2 dEC 10 67.37500
3 dEC 11 NA
4 dEC 12 65.88889
5 dEC 13 71.33333
6 dEC 14 69.87500
7 dEC 2 60.87500
8 dEC 3 65.75000
9 dEC 4 66.00000
10 dEC 5 64.50000
11 dEC 6 70.25000
12 dEC 7 66.75000
13 dEC 8 65.12500
14 dEC 9 68.75000
15 Sham - dEC 1 46.90909
16 Sham - dEC 10 67.54545
17 Sham - dEC 11 68.90909
18 Sham - dEC 12 70.00000
19 Sham - dEC 13 68.36364
20 Sham - dEC 14 71.27273
21 Sham - dEC 2 55.72727
22 Sham - dEC 3 62.09091
23 Sham - dEC 4 61.54545
24 Sham - dEC 5 66.09091
25 Sham - dEC 6 67.63636
26 Sham - dEC 7 66.09091
27 Sham - dEC 8 65.90909
28 Sham - dEC 9 65.81818
#Desired results
#Ordered means
Group rank mean
1 dEC 1 41.37500
7 dEC 2 60.87500
8 dEC 3 65.75000
9 dEC 4 66.00000
10 dEC 5 64.50000
11 dEC 6 70.25000
12 dEC 7 66.75000
13 dEC 8 65.12500
14 dEC 9 68.75000
2 dEC 10 67.37500
3 dEC 11 NA
4 dEC 12 65.88889
5 dEC 13 71.33333
6 dEC 14 69.87500
15 Sham - dEC 1 46.90909
21 Sham - dEC 2 55.72727
22 Sham - dEC 3 62.09091
23 Sham - dEC 4 61.54545
24 Sham - dEC 5 66.09091
25 Sham - dEC 6 67.63636
26 Sham - dEC 7 66.09091
27 Sham - dEC 8 65.90909
28 Sham - dEC 9 65.81818
16 Sham - dEC 10 67.54545
17 Sham - dEC 11 68.90909
18 Sham - dEC 12 70.00000
19 Sham - dEC 13 68.36364
20 Sham - dEC 14 71.27273
答案 0 :(得分:1)
rank
列不是数字。因此,我们将其转换为&#39;数字&#39;来自&#39;字符&#39;类和order
列&#39;组&#39;和&#39;排名&#39;
means[with(means, order(Group,as.numeric(rank))),]
或另一个选项是来自arrange
的{{1}}(由@Wistar评论)
plyr
如果我们使用 library(plyr)
arrange(means, Group, as.numeric(rank))
,则所有步骤都可以链接在一起(未经测试)
dplyr