Home > Net >  Formatting numeric values to time in R
Formatting numeric values to time in R

Time:09-21

I want to format my numeric sleep minutes in a data frame to time in R. For example, this is what the data frame looks like:

TotalMinutesAsleep 327

sleep.day.1$TotalMinutesAsleep <- hms(sleep.day.1$TotalMinutesAsleep)                         

I need a quick and easy solution.

Thx,

CodePudding user response:

If these values are in minutes, convert to seconds and use seconds_to_period

library(lubridate)
period1 <- seconds_to_period(sleep.day.1$TotalMinutesAsleep * 60)

then, we may format it with

sprintf('d:d:d', period1@hour, period1@minute, second(period1))

CodePudding user response:

We coul use hms part of the tidyverse: A simple class for storing time-of-day values

library(hms)
hms(sleep.day.1$TotalMinutesAsleep*60)

output:

05:27:00
  • Related