0

Hi I have a data frame (df) that looks like:

Date
Jun.1
Jan.1
Jul.3
Mar.1

It's a character type.

Trying to use the lubridate package to convert to a date type. However, the closest thing I can find is mdy which converts when the format is e.g. January 31st, 2017.

So, I tried to replace all the '.' to '' using gsub('.', '', df$date), but this just returns NAs.

Any advice?

1 Answers1

0

We can specify the truncated in mdy to make up for the missing component. It will return the current 'year' as it is missing

df$Date <- mdy(df$Date, truncated = 2)
df$Date
#[1] "2020-06-01" "2020-01-01" "2020-07-03" "2020-03-01"

data

df <- structure(list(Date = c("Jun.1", "Jan.1", "Jul.3", "Mar.1")),
class = "data.frame", row.names = c(NA, 
-4L))
akrun
  • 674,427
  • 24
  • 381
  • 486