How update the hour element whenever i sum exceeding minutes to lubridate object?

问题内容:

I want to add any quantity of minutes to a Period variable, and i’d like the hour element update whenever the minute element exceeds 59. In the following operation, the additive works well for the first example using this syntax, but in the second case it doesn’t:

library(lubridate)
hm_example <- hm("17:49")

hm_example + minutes(10)
# [1] "17H 59M 0S"

hm_example + minutes(100)
# [1] "17H 149M 0S"

The second case i’d like the result as 19H 29M 0S. I’ve seen the package documentation, but i couldn’t an alternative for this. Are there a native option from lubridate to correct it ?

I am using the lubridate version 1.7.1 .

问题评论:

答案:

答案1:

It is possible with a little trick and the argument roll = TRUE.

temp <- hm_example + minutes(100)

hms(temp, roll = TRUE)
#[1] "19H 29M 0S"

答案评论:

    
You don’t need as.character really.
    
You are right when using hms. Using hm will not work without as.character. It gives you NA.

原文地址:

https://stackoverflow.com/questions/47755454/how-update-the-hour-element-whenever-i-sum-exceeding-minutes-to-lubridate-object

Tags:,

添加评论

友情链接:蝴蝶教程