Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gavinsimpson opened this issue Jan 29, 2018 · 27 comments
Closed

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

gavinsimpson opened this issue Jan 29, 2018 · 27 comments

Comments

@gavinsimpson
Copy link

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)?

@vspinu
Copy link
Member

vspinu commented Jan 29, 2018

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.

@zenteknet
Copy link

I could use this function as well.

@johnvbusch
Copy link

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

@jenitivecase
Copy link

+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.

@zachmeador
Copy link

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.

@adanvr
Copy link

adanvr commented Jul 5, 2019

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

@Gillenwaters
Copy link

+1 in favor of this feature request

@lukerobert
Copy link

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.

@justinmillar
Copy link

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

@vspinu
Copy link
Member

vspinu commented Nov 13, 2019

I will add it soonish.

@dmontecino
Copy link

same here

@gavinsimpson
Copy link
Author

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

@jjphorn
Copy link

jjphorn commented Jul 3, 2020

This would be a great function to have.

@cjcarlson
Copy link

This would be good

@emileten
Copy link

Adding my vote as well !

@wrmadsen
Copy link

wrmadsen commented Aug 5, 2020

And my vote!

@thomasvargas
Copy link

+1

@joeytalbot
Copy link

I would also like to see this function!

@grhaonan
Copy link

grhaonan commented Oct 5, 2020

+1

1 similar comment
@dsolito
Copy link

dsolito commented Oct 12, 2020

+1

@MaLijia-thss15
Copy link

Looking forward to this function +1

@laketiticaca
Copy link

+1

1 similar comment
@ramiromagno
Copy link

+1

@vspinu vspinu reopened this Nov 10, 2020
@vspinu
Copy link
Member

vspinu commented Nov 10, 2020

Reopening due to a popular request.

@vspinu vspinu closed this as completed in 2709db7 Nov 10, 2020
@JustinKurland
Copy link

+1

1 similar comment
@sailagulhas
Copy link

+1

@vspinu
Copy link
Member

vspinu commented Dec 16, 2020

This feature is long on CRAN. Locking.

@tidyverse tidyverse locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests