简体   繁体   中英

R - Rounding with 'difftime' function

I have this times:
start.moment <- Sys.time() end.moment <- Sys.time()

To know the difference, I do:
difftime(end.moment, start.moment, units = "mins")

Time difference of 0.1501419 mins

But I want a result less extensive, like that (with the same units):

Time difference of 0.15 mins

What I have to change on last command?

You can use round to accomplish this:

round(difftime(end.moment, start.moment, units = "mins"), 2)

or the signif the significant number of digits:

signif(difftime(end.moment, start.moment, units = "mins"), 2)

will also work.

This is a part of print.difftime(...) , which is implicitly called every time you want to see the result of a difftime operation.

print(end.moment - start.moment, digits = 2)
Time difference of 15 mins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM