23

I have a vector "Time.Training" in the format hours:minutes:seconds (e.g.

Time.Training <- c("1:00:00", "0:45:00", "0:30:00", "1:30:00")

I would like to convert this into minutes in the format:

Time.Training.Minutes <- c(60, 45, 30, 90)

I'm wondering if someone has a straightforward method of doing this in R.

Many thanks.

Matt

Henrik
  • 65,555
  • 14
  • 143
  • 159
Matt Jordan
  • 567
  • 1
  • 5
  • 14
  • The Time.Training vector is being pulled in from Google Sheets using the url. It comes into R in the format hh:mm:ss. Hoping to convert this so I can calculate a training load for an athlete but I need this in minutes. – Matt Jordan Mar 15 '15 at 23:21

5 Answers5

23

Using lubridate:

Time.Training<- c("1:00:00", "0:45:00", "0:30:00", "1:30:00")

library(lubridate)
res <- hms(Time.Training)        # format to 'hours:minutes:seconds'
hour(res)*60 + minute(res)       # convert hours to minutes, and add minutes
## [1] 60 45 30 90
tospig
  • 7,762
  • 14
  • 40
  • 79
  • 1
    This worked. The exact code I used was: library(lubridate); res <- hms(load$Time.Spent.Training); load$Time.Minutes<-hour(res)*60 + minute(res). Thank you. – Matt Jordan Mar 15 '15 at 23:55
  • 1
    You're welcome. On your example given in the question @David Arenburg's method also works. If you want answers based on your actual data you should consider using `dput()` (at least on a subset of the data). – tospig Mar 16 '15 at 00:00
15

Try this. We basically converting to POSIXlt class first by pasting a real date to the vector using the Sys.Date() function (because there is no hour class in base R) and then using hour and min arguments in order to achieve the output

Res <- as.POSIXlt(paste(Sys.Date(), Time.Training))
Res$hour*60 + Res$min
## [1] 60 45 30 90
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 1
    The data table is called 'load' and the variable is called 'Time.Spent.Training'. I used the commands: attach(load) followed by Res <- as.POSIXlt(paste(Sys.Date(), Time.Spent.Training)). This gave a vector of dates 2015-03-15. I can't seem to get time from this – Matt Jordan Mar 15 '15 at 23:35
  • You need to call your column from your data set. Try `load$Time.Spent.Training` instaed of just `Time.Spent.Training`. – David Arenburg Mar 15 '15 at 23:44
11

Use as.difftime:

> Time.Training<- c("1:00:00", "0:45:00", "0:30:00", "1:30:00")
> strtoi(as.difftime(Time.Training, format = "%H:%M:%S", units = "mins"))
[1] 60 45 30 90
Perceptron
  • 399
  • 3
  • 11
10

Here are some alternatives:

1) The chron package has a "times" class in which 1 unit is a day and there are 60 * 24 minutes in a day so:

library(chron)
60 * 24 * as.numeric(times(Time.Training))

giving:

[1] 60 45 30 90

1a) Another approach using chron is the following (giving the same answer):

library(chron)

ch <- times(Time.training)
60 * hours(ch) + minutes(ch)

2) Here is an approach using read.table and matrix/vector multiplication. No packages are needed:

c(as.matrix(read.table(text = Time.Training, sep = ":")) %*% c(60, 1, 1/60))

(Using "POSIXlt" is probably the most straight-forward approach without packages but another answer already provides that.)

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
-2

Taking the hour column from the date time column and create a new cloumn hour and give only hour data in that column 2011-01-01 00:00:01 Ans :

bikeshare$hour<-sapply(bikeshare$datetime,function(x){format(x,"%H")})
tospig
  • 7,762
  • 14
  • 40
  • 79
Mano Ramu
  • 1
  • 2