Home > Blockchain >  Formatting a vector with inconsistent date formats in R
Formatting a vector with inconsistent date formats in R

Time:11-27

Im new to R and working on cleaning a data table and the csv has a column called Contract.Valid.Until

df$Contract.Valid.Until <- c("2020", "2021", "2019", "30-Jun-19", "2022", "18-Aug-2021")

I now want to convert it into a uniform date type, i found functions for the Year and for the Dates on Stack Overflow but not how to combine them, here are the functions i found

df$Contract.Valid.Until <- lubridate::ymd(df$Contract.Valid.Until, truncated = 2L)

df$Contract.Valid.Until <- as.Date(df$Contract.Valid.Until, "%d-%b-%y")

I would be very glad if any of you could help me :)

CodePudding user response:

Perhaps parse_date would work

library(parsedate)
parse_date(c("2020", "2021", "2019", "30-Jun-19", "2022", "18-Aug-2021"))
[1] "2020-01-01 UTC" "2021-01-01 UTC" "2019-01-01 UTC" "2019-06-30 UTC" "2022-01-01 UTC" "2021-08-18 UTC"
  • Related