假设我有这个示例 df 数据集
Order.Date
2011-10-20
2011-12-25
2012-04-15
2012-08-23
2013-09-25
Run Code Online (Sandbox Code Playgroud)
我想提取月份和年份,就像这样
Order.Date Month Year
2011-10-20 October 2011
2011-12-25 December 2011
2012-04-15 April 2012
2012-08-23 August 2012
2013-09-25 September 2013
Run Code Online (Sandbox Code Playgroud)
有什么解决办法吗?任何东西,可以使用 lubridate 或任何东西
lubridate month并且year会起作用。
as.data.frame(Order.Date) %>%
mutate(Month = lubridate::month(Order.Date, label = FALSE),
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 10 2011
2 2011-12-25 12 2011
3 2012-04-15 4 2012
4 2012-08-23 8 2012
5 2013-09-25 9 2013
Run Code Online (Sandbox Code Playgroud)
如果您希望月份格式为Jan,请使用month.abb和 as January,请使用month.name
as.data.frame(Order.Date) %>%
mutate(Month = month.abb[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 Oct 2011
2 2011-12-25 Dec 2011
3 2012-04-15 Apr 2012
4 2012-08-23 Aug 2012
5 2013-09-25 Sep 2013
as.data.frame(Order.Date) %>%
mutate(Month = month.name[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 October 2011
2 2011-12-25 December 2011
3 2012-04-15 April 2012
4 2012-08-23 August 2012
5 2013-09-25 September 2013
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4752 次 |
| 最近记录: |