r - 如果日期是字符,如何在 R 中安排日期?
问题描述
现在我有一个名为trending_month 的列,其中包含字符值:
'Jan 2018', 'Dec 2017', 'Feb 2018', 'Nov 2017'
我如何按升序排列它们以获得这个?
'Nov 2017', 'Dec 2017', 'Jan 2018', 'Feb 2018'
由于trending_month 是字符,当我按升序排序时,它按字母格式排列。
解决方案
一种方法是将值转换为 date 然后order
. 在基础 R 中,可以这样完成
df[order(as.Date(paste(1, df$trending_month), "%d %b %Y")), ]
# x trending_month
#4 4 Nov 2017
#2 2 Dec 2017
#1 1 Jan 2018
#3 3 Feb 2018
数据
df <- data.frame(x = 1:4,
trending_month = c('Jan 2018','Dec 2017','Feb 2018','Nov 2017'))
推荐阅读
- apache - Why does this mod_rewrite rewrite rule cause a loop?
- sql - 下周生日
- javascript - 以动态形式隐藏选择元素的问题
- pandas - 无法添加索引和替换标题行 - Pandas DataFrame
- c# - 在页面加载事件上单击(发布)按钮后刷新页面时下拉列表值不重复
- c# - 使用 linq lambda 获取总计数和总和
- c# - 从位图创建图像时 Emgu cv 间歇性异常
- html - 选中的复选框不会向数据库发送布尔值
- python - 在 OpenCV 中更改通道顺序可防止绘制矩形
- redirect - Weblogic将https重定向到http