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

Closed
mgacc0 opened this issue Oct 5, 2015 · 2 comments
Closed

maintain attributes of intervals when using Reduce #348

mgacc0 opened this issue Oct 5, 2015 · 2 comments

Comments

@mgacc0
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),
             interval=c(
               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"))
               ))
df
#   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]),
                 df$interval[3])
# [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: http://stackoverflow.com/questions/32944273/lubridate-how-to-calculate-the-union-of-many-intervals

@vspinu
Copy link
Member

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
@mgacc0
Copy link
Author

mgacc0 commented Oct 6, 2015

Thanks

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

No branches or pull requests

2 participants