Skip to content
Prev 387593 / 398502 Next

How to average minutes per hour per month in the form of '# hours #minutes'

Here is one approach:

tmp <- data.frame(min=seq(0,150, by=15))

tmp %>%
  mutate(hm=sprintf("%2d Hour%s %2d Minutes",
                    min %/% 60, ifelse((min %/% 60) == 1, " ", "s"),
                    min %% 60))

You could replace `sprintf` with `str_glue` (and update the syntax as
well) if you realy need tidyverse, but you would also loose some
formatting capability.

I don't know of tidyverse versions of `%/%` or `%%`.  If you need the
numeric values instead of a string then just remove the `sprintf` and
use mutate directly with `min %/% 60` and `min %% 60`.

This of course assumes all of your data is in minutes (by the time you
pipe to this code) and that all hours have 60 minutes (I don't know of
any leap hours.
On Sun, Mar 21, 2021 at 8:31 AM Dr Eberhard W Lisse <nospam at lisse.na> wrote: