Error with Time - "Origin" must be supplied

I have used the following to get the "Hour" from a "Time" variable and up to now it has been working well until to day.

dplyr::mutate(Hour = as.factor(hour(hms(Time)))) %>%

I am now getting the following error message:

32%20AM

What could be the problem and can i please have a solution to this.

Regards

lubridate::hms works on times stored as strings. What format is your Time variable? If it's a Date or POSIX already you don't need hms() just use hour() directly.

library(tidyverse)
library(lubridate)

df <- tibble(Time=Sys.time())

df %>% 
    mutate(Hour=as.factor(hour(Time)))
#> # A tibble: 1 x 2
#>   Time                Hour 
#>   <dttm>              <fct>
#> 1 2019-06-11 11:43:58 11

Created on 2019-06-11 by the reprex package (v0.3.0)

Many thanks for the response to my posting. My time is a factor as shown below:

$ Time : Factor w/ 740 levels "06:20:00.0000000"

Regards

I´m trying to replicate your issue with dates stored as factors but I can´t, It just works as expected.

library(lubridate)
library(dplyr)

# Made up sample data
df <- data.frame(Time = c("06:20:00.0000000", "07:20:00.0000000", "08:20:00.0000000"))
class(df$Time)
#> [1] "factor"

df %>% 
    mutate(Hour = as.factor(hour(hms(Time))))
#>               Time Hour
#> 1 06:20:00.0000000    6
#> 2 07:20:00.0000000    7
#> 3 08:20:00.0000000    8

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Many thanks for the solution, my problem is that i was using "Time" instead of a "Datetime". Most appreciated.

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