Lubridate: Add a ym() function for year-month "dates" in spirit of yq()?

Created on 29 Jan 2018  路  27Comments  路  Source: tidyverse/lubridate

As lubridate already has a handy yq() function for parsing year-quarter "dates", would you consider adding ym(), the equivalent for numeric year-month "dates"?

Date such as this crop up all the time in applied sciences, where the exact date of sampling is not recorded or neccessary or the data have been aggregated to a monthly time step, eg monthly climate anomolies, and provided in that format (i.e. without any day component).

parse_date_time knows how to handle such inputs

> parse_date_time("10/2001", "mY")
[1] "2001-10-01 UTC"

I'm thinking of something along the lines of (I'm new to lubridate so I'm not sure I follow the meaning of the orders argument just yet):

ym <- function (..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) {
  lubridate:::.parse_xxx(..., orders = "ym", quiet = quiet, tz = tz, locale = locale, 
                    truncated = 0)
}

my <- function (..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME")) {
  lubridate:::.parse_xxx(..., orders = "my", quiet = quiet, tz = tz, locale = locale, 
                    truncated = 0)
}

with expected output

> my("10-2001")
[1] "2001-10-01"
> ym("2010-10")
[1] "2010-10-01"

Optionally, accepting an argument fraction (or equivalent) would allow user to control the day part that is added: would return date + days(fraction * days_in_month)?

Most helpful comment

I will add it soonish.

All 27 comments

I don't think those functions are that useful, especially that the semantics of my and ym are likely not to be clear for a casual reader. Plus one would need yd, md etc. for consistency.

You can already do such partial parsing in a number of ways:

> ymd("2001-01", truncated = 1)
[1] "2001-01-01"
> myd("01-2001", truncated = 1)
[1] "2001-01-01"
> ymd(paste0("2001-01", "-03"))
[1] "2001-01-03"
> parse_date_time("2001-01", "ym")
[1] "2001-01-01 UTC"
> parse_date_time2("2001-01", "Ym")
[1] "2001-01-01 UTC"

For day part one can do x + days(3) or ceiling_date(x, "3d") etc.

I could use this function as well.

I was looking for this function today. Seems like all possible combinations of dates should be covered to make the library universally useful.

+1 in favor of this function. Writing out ugly solutions like mutate(month = paste0(year(date_col), "-", month(date_col))) is inelegant and seems contrary to the spirit of this package.

Just tossing my vote for support on this as well. Currently looking for an elegant way to incrementally increase MY and it's a bit of a pain to try to keep it clean and consistent with the other code utilizing Lubridate.

This is a really common use case at our work and would be a great add =)

+1 in favor of this feature request

Seems like floor_date covers most use-cases for this. For example,

dates <- as.Date(c("2019-01-01", "2019-02-20"))
floor_date(dates, "month")

Admittedly you get dates on the first of each month and not dates with no day component, but from that point on you can use all the standard Lubridate functions to add or subtract months.

Would also appreciate this feature, the workarounds are far less intuitive.

I will add it soonish.

same here

@vspinu Any update on this? Is there a timeline for "soonish" ;-)

This would be a great function to have.

This would be good

Adding my vote as well !

And my vote!

+1

I would also like to see this function!

+1

+1

Looking forward to this function +1

+1

+1

Reopening due to a popular request.

+1

+1

This feature is long on CRAN. Locking.

Was this page helpful?
0 / 5 - 0 ratings