6

I have a column of dates as follows,

> 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

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

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

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

Thanks~

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 4
    attach a day(01) to each and then you cn work on! else may be using `zoo` package – joel.wilson Dec 13 '16 at 13:49
  • @RonakShah expected output is a date variable that `lubridate` understands (so not a `character`) – ℕʘʘḆḽḘ Dec 13 '16 at 13:51
  • @DavidArenburg, the question you are referring to is 5 years old!! also, note that here I want the solution to be compatible with the tidyverse. So your flag is irrelevant – ℕʘʘḆḽḘ Dec 13 '16 at 13:56
  • The fact that this question is 5 years old only strengthens my point – David Arenburg Dec 13 '16 at 13:58
  • I dont agree. Many new packages, many new updates. Nobody wants to use an old, 5 year old solution when there may be something much easier and intuitive – ℕʘʘḆḽḘ Dec 13 '16 at 13:59
  • 2
    Yet, you accepted the exact same solution from the dupe from 5 years ago. And you call my flag "*irrelevant*" **. Let me make a wild guess here and say that joel Googled it for you and copied the solution from that dupe too. – David Arenburg Dec 13 '16 at 14:01
  • OK OK @DavidArenburg, your link to `ggfortify` convinced me. That may be a dupe - but still, I think 5 year is a loooot of time and people would find comforting to find this recent solution. Feel free to close it if you link and sorry for the hassle. Its always good to share constructive thoughts – ℕʘʘḆḽḘ Dec 13 '16 at 14:04
  • @DavidArenburg no need for downvoting me sob sob... – ℕʘʘḆḽḘ Dec 13 '16 at 14:08
  • I didn't downvote you. – David Arenburg Dec 13 '16 at 14:11

1 Answers1

14
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"

or another workaround,

as.Date(paste0("01/", mymonth),format = "%d/%m/%Y")
[1] "2015-10-01" "2016-11-01" "2016-12-01"
joel.wilson
  • 8,243
  • 5
  • 28
  • 48
  • thanks @joel.wilson. the `zoo` solution, it is compatible with `ggplot` and `lubridate`? – ℕʘʘḆḽḘ Dec 13 '16 at 13:54
  • didn't get the real purpose compatible with lubridate? never tried with ggplot2 package actually – joel.wilson Dec 13 '16 at 13:58
  • 3
    Again, [Google](http://search.r-project.org/library/zoo/html/ggplot2.zoo.html) – David Arenburg Dec 13 '16 at 14:00
  • 15
    Adding a bit of currency to this post: You could use lubridate's `parse_date_time` function as follows `format(lubridate::parse_date_time(mymonth, orders = c("m/Y")), "%m-%Y")` – Nick Apr 17 '20 at 07:55
  • 2
    The comment of Nick is the only thing here that actually answers the question for people trying to understand lubriate (who will end up here after googling). @joel.wilson do you want to update your answer? – Jakob May 04 '21 at 07:26