Class character to date while maintaining the format

51 Views Asked by At

I have the dataframe below:

a <- c("2019-07-23 00:00", "2019-07-23 01:00", "2019-07-23 02:00")
b <- c(1,2,3)
bind <- data.frame(a,b)

I would like to make a date column for column bind$a, but when I do this with the code below, the format is changed. When I try to change the format, the column class goes back to character...

bind$a <- format(as.POSIXct(bind$a,format='%Y-%m-%d %H:%M'),format='%Y-%m-%d %H:%M')
bind$a <- as.Date(bind$a, format = '%Y-%m-%d %H:%M')

How could I transform bind$a into a date column, while maintaining the format '%Y-%m-%d %H:%M'? Thanks!

0

There are 0 best solutions below