Reputation: 39
I have a data frame, with a column of type "character". It is formatted like: "2017-12-18 17:35:53"
I wish to convert this to a date format. I believed the following code would have worked, but it hasn't:
library(magrittr)
df1 %<>% mutate(responded_at= as.Date(column_name, format = "%d.%m.%Y"))
I'm not sure what else to try (I'm quite new to R).
Upvotes: 1
Views: 65
Reputation: 887851
The format "%d.%m.%Y"
implies two digit day followed by a dot (.
), then two digit month, followed by dot (.
) and a four digit year (%Y
- 4 digit year, %y
- 2 digit year). Based on the format of the data, it is 4 digit year followed by a dash -
, then two digit month, dash and two digit day followed by space and hour, minute, seconds in %H:%M:%S format. So, if we follow the correct format, it would be
library(dplyr)
library(magrittr)
df1 %<>%
mutate(responded_at= as.Date(column_name, format = "%Y-%m-%d %H:%M:%S"))
Fortunately, this format is also the default format for POSIXct
. So, we can directly use as.Date
without any format
df1 %<>%
mutate(responded_at= as.Date(column_name))
Upvotes: 1