I have a file containing date and time information as a character string. I want to convert it to a datetime POSIXlt object and to that end I use the strftime() base function in R.
While the command works fine when I am passing it a character string, or when I apply it a small slice of the dataframe, when I apply it in a larger slice or in the entire datadrame it fails to capture the Hour. Please see below:
d = "2017-11-18 01:00:00"
t = strftime(d, format = "%Y-%m-%d %H:%M")
t
'2017-11-18 01:00'
head(data %>% dplyr::slice(1:1000) %>% mutate(DateTime1 = strftime(DateTime, format = "%Y-%m-%d %H:%M")))
DateTime Junction Vehicles ID DateTime1
2015-11-01 00:00:00 1 15 20151101001 2015-11-01 00:00
2015-11-01 01:00:00 1 13 20151101011 2015-11-01 01:00
2015-11-01 02:00:00 1 10 20151101021 2015-11-01 02:00
2015-11-01 03:00:00 1 7 20151101031 2015-11-01 03:00
2015-11-01 04:00:00 1 9 20151101041 2015-11-01 04:00
2015-11-01 05:00:00 1 6 20151101051 2015-11-01 05:00
head(data %>% mutate(DateTime1 = strftime(DateTime, format = "%Y-%m-%d %H:%M")))
DateTime Junction Vehicles ID DateTime1
2015-11-01 00:00:00 1 15 20151101001 2015-11-01 00:00
2015-11-01 01:00:00 1 13 20151101011 2015-11-01 00:00
2015-11-01 02:00:00 1 10 20151101021 2015-11-01 00:00
2015-11-01 03:00:00 1 7 20151101031 2015-11-01 00:00
2015-11-01 04:00:00 1 9 20151101041 2015-11-01 00:00
2015-11-01 05:00:00 1 6 20151101051 2015-11-01 00:00
How this erratic behavior is to be explained and how can I convert the datetime column for the entire dataset?
Your advice will be appreciated.