Skip to content

as.period doesn't convert to specified time units if x is a vector #349

@mgacc0

Description

@mgacc0
ints <- c(
  new_interval(ymd("2001-01-01"), ymd("2002-01-01")),
  new_interval(ymd("2001-01-01"), ymd("2004-01-01"))
)
as.period(ints)
# [1] "1y 0m 0d 0H 0M 0S" "3y 0m 0d 0H 0M 0S"

as.period converts correctly to the specified time units if the variable is a scalar:

as.period(ints[1], "days")
# [1] "365d 0H 0M 0S"

But it shows an error if it's a vector:

as.period(ints, "days")
# Error in initialize(value, ...) : 
#   invalid names for slots of class “Period”: day1, day2

I would prefer not having to do something like

ans <- Map(function(x) as.period(x, "days"), ints)
ans
# [[1]]
# [1] "365d 0H 0M 0S"
# 
# [[2]]
# [1] "1095d 0H 0M 0S"

And, also, why does this happen?

unlist(ans)
# [1] 0 0

Should be necessary to implement the unlist function for Interval objects?

(Thanks in advance.)

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