Error in creating a new hms variable based on a character variable

I am rerunning my code from more than one year ago, which tries to create an hms variable based on another character variable in the dataset. But I now get an error saying:

Error in mutate():
:information_source: In argument: x.time = case_when(x == "a few minute" ~ hms(minutes = 5), TRUE ~ as_hms(x)).
Caused by error in case_when():
! Failed to evaluate the right-hand side of formula 2.
Caused by error in abort_lossy_cast():
! Lossy cast from to at position(s) 1, 2
Backtrace:

  1. df.test %>% ...
  2. hms:::as_hms.default(x)
  3. vctrs::vec_cast(x, new_hms())
  4. vctrs (local) <fn>()
  5. hms:::vec_cast.hms.character(...)
  6. hms:::abort_lossy_cast(x, to, ..., lossy = lossy)

Below is an example code gets the same error:

df.test <- data.frame(x = c("a few minute", "1"))

df.test <- df.test %>% mutate(x.time = case_when(x == "a few minute" ~ hms(minutes = 5),  TRUE ~ as.hms(x)))

I am guessing this is caused by the update of the package and I probably was using an older version that made the code work. What can I do to fix the error under v1.1.3?

The error is simply that as_hms doesn't understand what to do when you give it the text '1'

as_hms("1")
# Error in `abort_lossy_cast()`:
# ! Lossy cast from <character> to <hms> at position(s) 1

what time do you intend for that to represent ? 1 second ? if so you could readr::parse_integer() it to have it be an integer rather than a string because as_hms(3) would be 3 seconds i.e. 00:00:03

I think it used to be converted to minutes, but I can make it work by multiplying with 60. Thanks a lot!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.