Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting Timestamp from chr to Time/Data-Type in R

I started working with R and I have a small problem with Timestamps.
I am cleaning my Data for a case study and I don’t like to see the Timestamps classified as a chr Datatype.

$ time         <chr> "00:00:00", "01:00:00", "02:00:00", "03:00:00", "04:00:00…

I want to convert them to an adequate data type.
I am first of all confused about the right data type for this information.
I saw people converting them into a date, which does not seem right to me?

I tried different strategies that I saw online but I was just not successful.
There must be an easy way to convert this data to another data type.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If someone could help me, I would be very thankful.

>Solution :

These could be represented as chron "times" objects (which are internally represented as a fraction of a day) or lubridate "Period" S4 objects. Externally they are rendered as shown.

ch <- c("00:00:00", "01:00:00", "02:00:00", "03:00:00")

library(chron)
times(ch)
## [1] 00:00:00 01:00:00 02:00:00 03:00:00
 
library(lubridate)
hms(ch)
## [1] "0S"       "1H 0M 0S" "2H 0M 0S" "3H 0M 0S"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading