Home > Back-end >  Convert character to datetime object produces NAs
Convert character to datetime object produces NAs

Time:09-26

I have this character vector but when I try to convert it to datetime object I get NAs

timess<-c("11.01.2021 04:40", "03.01.2021 05:45", "30.12.2020 02:28", 
"02.01.2021 08:13", "03.01.2021 05:45", "04.01.2021 04:33", "03.01.2021 05:45", 
"02.01.2021 08:13", "03.01.2021 05:45", "02.01.2021 08:13")

timess<-as.POSIXct(timess, format="%d.%m.%y %H:%M:%S")

CodePudding user response:

paste the seconds to the strings, and take good care of the format string, see?strptime.

as.POSIXct(paste0(timess, ':00'), format="%d.%m.%Y %H:%M:%S")
# [1] "2021-01-11 04:40:00 CET" "2021-01-03 05:45:00 CET"
# [3] "2020-12-30 02:28:00 CET" "2021-01-02 08:13:00 CET"
# [5] "2021-01-03 05:45:00 CET" "2021-01-04 04:33:00 CET"
# [7] "2021-01-03 05:45:00 CET" "2021-01-02 08:13:00 CET"
# [9] "2021-01-03 05:45:00 CET" "2021-01-02 08:13:00 CET"
  • Related