Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maintain attributes of intervals when using Reduce #348

mgacc0 opened this issue Oct 5, 2015 · 2 comments

maintain attributes of intervals when using Reduce #348

mgacc0 opened this issue Oct 5, 2015 · 2 comments


Copy link

mgacc0 commented Oct 5, 2015

I want to obtain the union of many (more than 2) intervals:

df <- data.frame(id=c(1, 2, 3),
               new_interval(ymd("2001-01-01"), ymd("2002-01-01")),
               new_interval(ymd("2001-01-01"), ymd("2004-01-01")),
               new_interval(ymd("2001-02-01"), ymd("2002-01-01"))
#   id                       interval
#1  1 2001-01-01 UTC--2002-01-01 UTC
#2  2 2001-01-01 UTC--2004-01-01 UTC
#3  3 2001-02-01 UTC--2002-01-01 UTC

lubridate::union(lubridate::union(df$interval[1], df$interval[2]),
# [1] 2001-01-01 UTC--2004-01-01 UTC

That's the correct result.

But why lubridate::union does not work with Reduce?

Reduce(lubridate::union, df$interval )
# [1] 31536000 94608000 28857600

The intervals objects seem to get converted to numeric too son (before applying the union).

Related to:

Copy link

vspinu commented Oct 5, 2015

Reduce calls as.list on the input, so that lubridate's union is not even called on those inputs.

There are two approaches, either extend union to accept additional arguments and handle this case internally, or implement as.list( method for interval objects.

@vspinu vspinu closed this as completed in 166db8a Oct 5, 2015
Copy link

mgacc0 commented Oct 6, 2015


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

2 participants