Help with Date Column

Hi Team,

I have a column stating month and year "Jan-14" as seen below

image

I would like to convert it to "1/1/2014". As of now it is a character type. When I use as.Date(), all the values change to NAs. I wanted to use lubridate() also, but I am doing something wrong that it doesn't work at all.

Any idea as to how we can convert the column to Date type and with formatting as 1/1/2014.

Thanks for your help!

Regards,
Kamlesh

I had to add a prefix of 01 to the dates to convert them with lubridate.

I do not know how to format the date within the data frame and I cannot think of a reason to do it. If you want to format the character representation of the date, you can use the format function.

library(lubridate)

DF <- data.frame(Year = c("Jan-14", "Feb-14"), stringsAsFactors = FALSE)
DF$FullDate <- paste0("01-", DF$Year)
DF$FullDate <- dmy(DF$FullDate)
DF
#>     Year   FullDate
#> 1 Jan-14 2014-01-01
#> 2 Feb-14 2014-02-01
format(DF$FullDate, "%d/%m/%Y")
#> [1] "01/01/2014" "01/02/2014"

Created on 2020-06-08 by the reprex package (v0.3.0)

Thanks @FJCC! This does the trick!

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