Converting hour, minutes columns in dataframe to time format
datetime-conversion, r, time-format, type-conversion
Solution
You could use `ISOdatetime` to convert the numbers in the `hour` and `min` to a `POSIXct` object. However, a POSIXct object is only defined when it also includes a year, month and day. So depending on your needs to can do several things:
- If you need a real time object which is correctly printed in graphs for example and can be used in arithmetic (addition, subtraction), you need to use `ISOdatetime`. `ISOdatetime` returns a so called `POSIXct` object, which is an R object which represents time. Then in `ISOdatetime` you just use fixed values for `year`, `month`, and `day`. This ofcourse only works if your dataset does not span multiple years.
- If you just need a character column `Time`, you can convert the `POSIXct` output to string using `strftime`. By setting the `format` argument to `"%H:%M:00"`. In this case however, you could also use `sprintf` to create the new character column without converting to `POSIXct`: `sprintf("%s:%s:00", drame$Hour, drame$Min)`.
Problem
I have a dataframe containing 3 columns including minutes and hours. I want to convert these columns (namely minutes and column) to time format. Given the data in `drame`: ``` Score Hour Min 10 10 56 23 17 01 ``` I would like to get: ``` Score Time 10 10:56:00 23 17:01:00 ```