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

Time:02-27

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.

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

CodePudding user response:

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"
  • Related