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
You could use
ISOdatetimeto convert the numbers in thehourandminto aPOSIXctobject. 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:ISOdatetime.ISOdatetimereturns a so calledPOSIXctobject, which is an R object which represents time. Then inISOdatetimeyou just use fixed values foryear,month, andday. This ofcourse only works if your dataset does not span multiple years.Time, you can convert thePOSIXctoutput to string usingstrftime. By setting theformatargument to"%H:%M:00". In this case however, you could also usesprintfto create the new character column without converting toPOSIXct:sprintf("%s:%s:00", drame$Hour, drame$Min).