help with "Error in as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format."

Hi Mishabalyasin,

thanks for your code, i tried using your code to convert one of my variables as well, but im getting an error. I am a beginner data scientist so please forgive my easy mistakes.

my time variable is formatted:
06/28/2019 05:09:07 PM

My code is below:

transitdata <- transitdata %>%
  mutate(year = year(Time), 
         month = month(Time), 
         day = day(Time),
         hour = hour(Time),
         minute = minute(Time),
         second = second(Time)
         )

Error in as.POSIXlt.character(x, tz = tz(x)) : character string is not in a standard unambiguous format.

I suspect your Time is being read as a character and needs to be converted to a timestamp with the lubridate::mdy_hms() function.

library(dplyr)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
df <- data.frame(Time = "06/28/2019 05:09:07 PM") #Time is read as character
df
#>                     Time
#> 1 06/28/2019 05:09:07 PM
df <- df %>% mutate(Time = mdy_hms(Time)) #convert Time to a timestamp
df
#>                  Time
#> 1 2019-06-28 17:09:07
df <- df %>% mutate(Year = year(Time), 
                    Month = month(Time), 
                    Day = day(Time),
                    hour = hour(Time),
                    minute = minute(Time),
                    second = second(Time))
df
#>                  Time Year Month Day hour minute second
#> 1 2019-06-28 17:09:07 2019     6  28   17      9      7

Created on 2019-07-31 by the reprex package (v0.2.1)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.