How to convert time difference into minutes in R?

烈酒焚心 提交于 2021-02-18 20:26:54

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29453742/how-to-convert-time-difference-into-minutes-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!