Imputing partial end dates

First, my apologies for what is probably a very basic question, but I'm an absolute beginner with R. Also, apologies if I use incorrect terminology; I'm a SAS programmer and tend to use those terms.

I have a character date variable that may contain year only ("2020"), or year and month ("2020-11"), or may be a complete date ("2020-11-24"). I need to impute the incomplete dates to the end of the year if we have year only ("2020" becomes "2020-12-31"), or to the end of the month if we have year and month only (which of course will depend on which month it is; and if February, also what year it is).

Please tell me there's a quick and easy way to do this. I'm not concerned about the year-only dates, since that would be simply pasting on "-12-31" in all cases. I figured out how to substring out the month for the year + month dates, but am having a hard time coming up with a way to use this in an if-else situation . And I'm not even looking at dealing with February yet...that's why I'm hoping there's an existing function, or some easy way to do this.

Thank you!!

here is a starting solution


library(tidyverse)
library(lubridate)
(my_df <- tribble(
  ~mychardate,
  "2014-12-31",
  "2014-12",
  "2014",
  "2020-02-29",
  "2020-02",
  "2020"
))

(my_df2 <- mutate(rowwise(my_df),
  raw_parts = str_split(mychardate, pattern = "-"),
  parts = length(raw_parts),
  my_date = case_when(
    parts == 3 ~ ymd(mychardate),
    parts == 2 ~ ceiling_date(
                  ymd(paste0(mychardate, "-01")),
                    unit = "month") - 1,
    parts == 1 ~ ymd(paste0(mychardate, "-12-31"))
  )
))
1 Like

You are now officially my new favorite person in the universe. It worked PERFECTLY!! Exactly what I needed.

I cannot thank you enough!!! :grin:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.