Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

datetime - How can I round seconds to 2 places upon using R package lubridate's seconds_to_period function?

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...