gpt4 book ai didi

r 顺序数值

转载 作者:行者123 更新时间:2023-12-05 05:25:12 26 4
gpt4 key购买 nike

我的问题很简单,但有些问题我找不到如何对数字进行排序。我希望它按组和等级排序 (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 列不是数字。因此,我们将其从“字符”类转换为“数字”,并排序“Group”和“rank”列

 means[with(means, order(Group,as.numeric(rank))),]

或者另一种选择是 arrange from plyr(正如@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))

关于r 顺序数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31908921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com