I am working with a dataset in R named "may_2020_v1" that contains a column named "ride_time" in which the time observations are in character data type. I want to convert these observations into the standard time format of HH:MM:SS. Can anyone please help me out?
Asked
Active
Viewed 62 times
0
-
1It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Images are not the right way to share data/code. – Ronak Shah May 29 '21 at 09:09
-
1I tried to answer your question in a general way, since not much information was provided. I just noticed your screenshot. This is one way of providing information, but imo the worst way, since people cannot simply copy your data and test what they think could be the solution to your problem. For future questions, use the ```dput()``` function and copy-paste the output to your question (also look at how to format questions on stackoverflow). If you do it this way, people here can understand the problem better and give better answers. – Manuel Popp May 29 '21 at 09:27
2 Answers
1
If you want to convert a string like "09:37:46" to a time datatype, you can use e.g. the as.POSIXct()
function described here. Depending on how you wrote the times in your strings, you can set the format
option to make the function recognise your time format.
For example*:
as.POSIXct("11:28:31", format = "%H:%M:%S")
Another option would be the strftime()
(description here).
*note that in this case, the function will have to make some assumptions, e.g., the date and time zone, since those were not provided in the string. It would be better to call the function like this: as.POSIXct("2021-05-29 11:28:31 CEST")
.
If you don't have a date for your time, you can also have a look at this and similar questions on stackoverflow.

Manuel Popp
- 1,003
- 1
- 10
- 33
1
The {hms}
package can help here:
library(hms)
times <- hms::parse_hms(c("10:3:52", "10:47:11", "16:27:36", "13:17:24"))
times
10:03:52
10:47:11
16:27:36
13:17:24

Ray
- 2,008
- 14
- 21