ragesz ragesz - 1 year ago 64
R Question

R convert dataframe string column to datetime with one-digit length hour

How can I convert a "string" dataframe column

into datetime when my hour can be one digit length too (eg.:
2012.01.01 9:00:00
)? The following code returns only with the date part without time part:

df[['attr_date']] <- as.Date(df[['attr_date']], format='%Y.%m.%d %H:%M:%S')

So the result of
2012.01.01 9:00:00

Bonus question:
Using Anaconda R Essentials, in Jupyter R Notebook why I get
as the result of
2012.01.01 9:00:00
when I use the above code?

Thank you!

Answer Source

Try using strptime instead:

df[['attr_date']] <- strptime(df[['attr_date']], format='%Y.%m.%d  %H:%M:%S')