gpt4 book ai didi

r - 在 R 中对 "year/mon"列进行排序

转载 作者:行者123 更新时间:2023-12-01 23:59:23 24 4
gpt4 key购买 nike

我试图在 R 中按“year.month”列对数据框进行排序,但停留在“as.Date”函数。我尝试了其他几种方法但没有成功。我能得到一些帮助吗?这里是。我的代码是

temp2_sort <- temp2[with(temp2, order(as.Date(year.month, format = "%y-%m"))),]
or
temp2_sort <- temp2[with(temp2, order(as.Date(year.month, format = "%y-%b"))),]

但它们都不起作用。

谢谢!

year.month  sale
2006/2 437
2006/3 52299
2006/9 175983
2006/12 57560
2007/2 10798
2007/3 12926
2006/5 61039
2006/8 135601
2006/6 54336
2006/10 72052

最佳答案

使用lubridate:

 dat[order(ymd(dat$year.month)),]

使用 as.Date 你应该添加一个虚拟的日期部分:

dat[order(as.Date(paste0(dat$year.month,'/1'),"%Y/%m/%d")),]

关于r - 在 R 中对 "year/mon"列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22257450/

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