Skip to content

Convert Period to numeric #420

@etiennebr

Description

@etiennebr

I couldn't find the right way to do it, but it seems hard to convert Period elements to numeric.
This is the expected behavior (as.numeric on difftime)

as.numeric(as.difftime("24:00:00"), "hours")
# [1] 24
as.numeric(as.difftime("24:00:00"), "mins")
# [1] 1440

However Period objects only return the unitpart of the Period, like components getters (second, minute, ... )

as.numeric(period(24, "hours"), "mins")
# [1] 0  # This is unexpected to me, I believe it's an error
as.numeric(period(24, "hours") + period(10, "minutes"), "mins")
# [1] 10
as.numeric(period(24, "hours"), "hours")
# [1] 24

The trick I found was to convert to duration, but I get a warning when converting Period to duration.

as.numeric(as.duration(period(10, "minutes")), "mins")
# estimate only: convert periods to intervals for accuracy
# [1] 10
as.numeric(as.duration(period(10, "minutes")), "hours")
# estimate only: convert periods to intervals for accuracy
# [1] 0.1666667

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions