gpt4 book ai didi

r - 使用 dplyr 中的排列()按时间顺序对月份进行排序

转载 作者:行者123 更新时间:2023-12-04 17:18:38 24 4
gpt4 key购买 nike

我有一个我转换为名称的月份(数字)列表:

fd <- df %>%
select(product, sales, month) %>%
mutate(month = month.name[month]) %>%
filter(!is.na(product), sales!=0) %>%
group_by(month) %>%
summarise(sales = sum(sales)) %>%
collect()

我想对表格进行排序,以便按时间顺序显示月份。我正在寻找解决方案 arrange()来自 dplyr如果可能的话。

这是 fd的结果:
       month   sales
1 April 1306629
2 August 1317986
3 December 1263070
4 February 1493914
5 January 1316889
6 July 1323161
7 June 1331614
8 March 1439019
9 May 1369881
10 November 1256950
11 October 1317647
12 September 1229632

最佳答案

这种方法怎么样:

set.seed(12)
df <- data.frame(month = sample(12), x = LETTERS[1:12])
df
# month x
#1 1 A
#2 9 B
#3 10 C
#4 3 D
#5 2 E
#6 12 F
#7 8 G
#8 4 H
#9 7 I
#10 5 J
#11 11 K
#12 6 L

library(dplyr)
df %>%
mutate(month = factor(month.name[month], levels = month.name)) %>%
arrange(month)

# month x
#1 January A
#2 February E
#3 March D
#4 April H
#5 May J
#6 June L
#7 July I
#8 August G
#9 September B
#10 October C
#11 November K
#12 December F

关于r - 使用 dplyr 中的排列()按时间顺序对月份进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27493897/

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