[R] Converting a number of minutes to a difftime

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 12 23:15:18 CEST 2005

On Thu, 12 May 2005, Martin C. Martin wrote:

> I have a variable "m" that contains the number of minutes that something 
> lasted, e.g.
> m <- 139
> I'd like to convert it to a difftime, so I can add it to the POSIXct start 
> time.  But as.difftime seems to want a character string, with at most two 
> characters for the minutes.  All other conversion functions seem to take 
> strings as well.  What am I missing?

as.difftime() is quite limited.  You need to get your hands dirty:

    structure(m, units = "mins", class = "difftime")

will do the job.

Alternatively,  Sys.time + (m*60) works.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list