Converting Time from Character to Numerical

Hello dear R-Forum,

I have a question regarding converting a character variable to a numeric variable so I can use it later in my analyses.
I have measured the duration of certain behaviors of individuals in an experiment. These are in the following form: hour:minutes:seconds.milliseconds (E.g. 00:00:03.792).
I want to be able to add up all the times for each subject at the end.

My data set (data1) looks something like this:

image

So I want to convert the last two columns that are in character to a numeric variable.

I have already tried a few things but unfortunately always without success.

My attempts so far:

1st attempt:
as.numeric(sub(":", ".", substr(data1$stress_duration,1,4365)), na.rm=TRUE)
--> Nothing happens with this command except a small warning message:
"NAs introduced by coercion"

2nd attempt:
data1$stress_duration <- as.numeric(sub("^(\d+):(\d+).*", "\1.\2", data1$stress_duration))
--> here it shows me that the variable is now numeric, but all times are set to zero

3rd attempt:
data1$stress_duration<-as.numeric(data1$stress_duration, na.rm=TRUE)
--> again I get a message that the variable is now numeric, but all times are set to NA.

4th attempt:
data1$stress_duration <- strptime(data1$stress_duration, format = "%H:%M:%OS")
options(digits.secs = 3)
--> here now also a date is inserted and also with this format I again can't sum up

5th attempt:
data1$stress_duration$gsub("*\:[0-9]", ".", data1$stress_duration)
--> here also nothing happens except an error message:
"This error occurs when you attempt to access an element of an atomic vector using the $ operator."

Does anyone of you have a solution?

Thanks in advance

Sarah

Using the as_hms() function from hms seems to work if you convert the "0" values to "00:00:00"

library(hms)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
DF <- data.frame(stress_duration = c("0","0","00:00:02.625", "00:00:02.153"))
DF <- DF |> mutate(stress_duration = ifelse(stress_duration == "0", 
                                            "00:00:00", stress_duration),
                   stress_duration = as_hms(stress_duration))
sum(DF$stress_duration)
#> Time difference of 4.778 secs

Created on 2022-12-18 with reprex v2.0.2

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.