lubridate:如何解析月-年? [英] lubridate: how to parse month-year?

查看:115
本文介绍了lubridate:如何解析月-年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列日期如下,

> mymonth = c('10/2015','11/2016','12/2016')
> data <- data_frame(mymonth)
> data
# A tibble: 3 × 1
  mymonth
    <chr>
1 10/2015
2 11/2016
3 12/2016

很显然,这里的月份对应于一年中的特定月份. 2015年10月,2015年11月和2015年12月.

Here, obviously, my month corresponds to a particular month in the year. October 2015, November 2015 and December 2015.

我无法使用lubridate正确解析这些日期.假定月份对应于该月的最后一个工作日.

I cannot parse these dates correctly with lubridate. It is assumed the month correspond to the last business day of the month.

如何将此变量转换为lubridate可以理解的date变量?

How can I have this variable translated into a date variable that lubridate understands?

谢谢〜

推荐答案

library(zoo)
as.yearmon(mymonth, "%m/%Y")
[1] "Oct 2015" "Nov 2016" "Dec 2016"

as.Date(as.yearmon(mymonth, "%m/%Y"))
[1] "2015-10-01" "2016-11-01" "2016-12-01"

或其他解决方法,

as.Date(paste0("01/", mymonth),format = "%d/%m/%Y")
[1] "2015-10-01" "2016-11-01" "2016-12-01"

这篇关于lubridate:如何解析月-年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆