我试图在 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
Using lubridate
:
dat[order(ymd(dat$year.month)),]
Using as.Date
你应该添加一个虚拟的一天部分:
dat[order(as.Date(paste0(dat$year.month,'/1'),"%Y/%m/%d")),]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)