Search code examples
rtidyverse

Converting irregular dates in R and the Tidyverse


I have a series of dates as follows

25 September 2019
27 April 2020
1994
28 February 2021
1986

Now I want to convert the 1994 and 1996 to:

01 January 1994
01 January 1986

Other full dates should be left as they are.

Any help is appreciated especially using the tidyverse way.


Solution

  • Given some vector d of dates and years:

    > d
    [1] "25 September 2019" "27 April 2020"     "1994"             
    [4] "28 February 2021"  "1986"             
    

    Replace any entries with only 4 letters with those four letters with "01 January" pasted in front:

    > d[nchar(d)==4] = paste0("01 January ",d[nchar(d)==4])
    

    Giving:

    > d
    [1] "25 September 2019" "27 April 2020"     "01 January 1994"  
    [4] "28 February 2021"  "01 January 1986"