How can I turn a column with HMS format into seconds?

I uploaded a file too big for excel into rstudio, I'm pretty new at it
It has a column with durations in hh mm ss format, that Rstudio detects as class "hms" "difftime"
I can't use period_to_seconds because I get this error

trying to get slot "minute" from an object (class "hms") that is not an S4 object

And if I try to create a new column called duration with that info in seconds, using this

q2$duration <- as.numeric(difftime(q2$2nd_day,q2$1st_day))
I get this error
Error in as.POSIXlt.character(x, tz, ...) : **
** character string is not in a standard unambiguous format

2nd_day and 1st_day have info in ymd_hms format, but if I use class on it it tells me "character"

Anybody has an idea on how to solve this? I'm a bit stuck Thanks to everybody

Hi Walter, welcome to the forum.

I think we need to have a look at a bit of your data. Could you issue the command:

dput(head(my_data, 20) 

where my_data is the name of your dataset
and copy the output to here?

@jrkrideau is right—a reprex is the way to go. But, as a New Year special, the data is not to hard to make up.

#> Loading required package: timechange
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>     date, intersect, setdiff, union

(begin  <- "2022-12-31 04:07:31")
#> [1] "2022-12-31 04:07:31"
(finish <- "2023-01-01 14:07:56")
#> [1] "2023-01-01 14:07:56"
(begin <- ymd_hms(begin))
#> [1] "2022-12-31 04:07:31 UTC"
(finish <- ymd_hms(finish))
#> [1] "2023-01-01 14:07:56 UTC"
(lapsed <- as.duration(interval(begin,finish, tzone = tz(begin))))
#> [1] "122425s (~1.42 days)"
(just_seconds <- lapsed@.Data)
#> [1] 122425

find_seconds <- function(x,y){
  begin  = ymd_hms(x)
  finish = ymd_hms(y)
  lapsed = as.duration(
    interval(begin,finish, tzone = tz(begin)))
  just_seconds = lapsed@.Data
#> [1] 122425

This topic was automatically closed 7 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.