r 顺序数值
r order numeric values
我的问题很简单,但有些问题我找不到如何对数字进行排序。我希望它按组和等级排序 (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
rank
列不是数字。因此,我们将其从 'character' class 和 order
列 'Group' 和 'rank'
转换为 'numeric'
means[with(means, order(Group,as.numeric(rank))),]
或者另一个选项是 plyr
中的 arrange
(正如@Wistar 所评论的)
library(plyr)
arrange(means, Group, as.numeric(rank))
如果我们使用 dplyr
,所有步骤都可以链接在一起(未测试)
library(dplyr)
Data %>%
group_by(Group, rank) %>%
summarise(mean=mean(Foo)) %>%
arrange(Group, as.numeric(rank))
我的问题很简单,但有些问题我找不到如何对数字进行排序。我希望它按组和等级排序 (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
rank
列不是数字。因此,我们将其从 'character' class 和 order
列 'Group' 和 'rank'
means[with(means, order(Group,as.numeric(rank))),]
或者另一个选项是 plyr
中的 arrange
(正如@Wistar 所评论的)
library(plyr)
arrange(means, Group, as.numeric(rank))
如果我们使用 dplyr
,所有步骤都可以链接在一起(未测试)
library(dplyr)
Data %>%
group_by(Group, rank) %>%
summarise(mean=mean(Foo)) %>%
arrange(Group, as.numeric(rank))