使用R包lubridate的seconds_to_period函数时,如何将秒数舍入到2位? [英] How can I round seconds to 2 places upon using R package lubridate's seconds_to_period function?

查看:75
本文介绍了使用R包lubridate的seconds_to_period函数时,如何将秒数舍入到2位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R包Lubridate的函数 seconds_to_period 将秒转换为DD:HH:MM:SS 格式.现在,我有很多类似的时间列表-> 2d 7H 1M 5.31500000000233S .我需要将秒数舍入到小数点后2位.
我尝试使用 round_date ymd_hms ,但是我在as.POSIXct.numeric(x)中收到此错误:必须提供'origin'.我试过是否可以分别访问秒并编写函数来更改秒,但不能.我该如何解决?

I converted seconds to DD:HH:MM:SS format using the R package Lubridate's function seconds_to_period. Now I have a huge list of times which look like this -> 2d 7H 1M 5.31500000000233S. I need to round off the seconds to 2 decimal places.
I tried using round_date and ymd_hms but I receive this error in as.POSIXct.numeric(x) : 'origin' must be supplied. I tried if I could access the seconds separately and write a function to change them but could not. How can I solve this?

为参考数据附加了duration.RData文件和durdata.R文件.这两个文件具有相同的数据,但是一个使用dput(),另一个使用save().链接:链接到文件

Attached a duration.RData file and durdata.R file for reference data. Both the files have the same data but one is using dput() and other using save(). Link : link to files

推荐答案

我们可以使用 round 格式化句点"对象中的小数位.

We can use round to format decimal places in "Period" objects.

head(round(time, 2))
#[1] "2d 7H 1M 5.32S"   "1d 9H 20M 37.49S" "7H 39M 37.95S" "2d 5H 0M 41.01S" 
#    "1d 8H 32M 2.8S"   "6H 36M 25.19S" 

这篇关于使用R包lubridate的seconds_to_period函数时,如何将秒数舍入到2位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆