在R中对“年/月”列进行排序

用户3393752

我试图按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")),]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章