Skip to content

implement as.integer method for durations, or make it fail #1055

@mkoohafkan

Description

@mkoohafkan

duration objects have a method for as.numeric() but not as.integer(). Using as.integer() on a duration object returns a value but does not respect the units argument (as can be passed to as.numeric()) or warn that the units are specification is ignored. I think it would be worth implementing an as.integer() method just for safety; I've made this error myself and come across it in other peoples' code as well.

library(lubridate)

start = as_datetime("2022-07-07 13:30:00")
end = as_datetime("2022-07-07 13:45:30")

# correct
as.numeric(as.duration(end - start), "seconds")
#> [1] 930
as.integer(as.duration(end - start), "seconds")
#> [1] 930

# correct
as.numeric(as.duration(end - start), "minutes")
#> 15.5

# but this is wrong
as.integer(as.duration(end - start), "minutes")
#> 930

# this is correct
as.integer(as.numeric(as.duration(end - start), "minutes"))
#> 15

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions