I have the following vector:

`vec <- c("01:10","12:42","1:43:02")`

I would like to know how to convert it into a **numeric vector** counting seconds from 00:00. Does anyone have an idea on how to proceed?

Thanks

I have the following vector:

`vec <- c("01:10","12:42","1:43:02")`

I would like to know how to convert it into a **numeric vector** counting seconds from 00:00. Does anyone have an idea on how to proceed?

Thanks

```
library(hms)
ifelse(is.na(parse_hms(vec)),
parse_hm(vec),
parse_hms(vec))
```

1 Like

Thanks for the answer. However, it only worked for "1:43:02"

```
vec <- c("01:10","12:42","1:43:02")
ifelse(is.na(parse_hms(vec)),
parse_hm(vec),
parse_hms(vec)) parse_hms(vec))
[1] 4200 45720 6182`
I'm not sure why, but I will keep trying.
```

01:10 is 4200 seconds.

3600+10*60 seconds

1 Like

Sorry, I should have been more clear. "01:10" refers to 0 hours, 1 minute, and 10 seconds.

```
splts <- str_count(vec,":")
vec2 <- ifelse(splts==1,
paste0("0:",vec),
vec)
library(hms)
parse_hms(vec2) %>% as.integer()
```

1 Like

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.