Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert time difference into minutes in R?

Tags:

r

I have the following program:

timeStart<-Sys.time()
timeEnd<-Sys.time()
difference<-timeEnd-timeStart
anyVector<-c(difference)

at the end I need to put that data into a vector, the problem that I have is than when the difference is in seconds the value is like:

4.46809 seconds

and when it passes some minutes the values is like:

2.344445 minutes

I would like that the answer is converted to minutes in any case, but when I do something like this:

anyVector<-c(difference/60)

for the case that the value of the difference is in seconds work fine, but it will also transform the data when is in minutes giving me an incorrect number.

So, how I can convert to minutes only when the answer is in seconds and not where is in minutes already?

like image 822
Little Avatar asked Apr 05 '15 03:04

Little


1 Answers

You can do it with the difftime function from base R:

timeStart<-Sys.time()
timeEnd<-Sys.time()

difference <- difftime(timeEnd, timeStart, units='mins')

Output

> difference
Time difference of 0.1424748 mins

You just specify the units argument and set it to mins and the output will always be in minutes.

like image 89
LyzandeR Avatar answered Nov 03 '22 01:11

LyzandeR