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

Comments

Projects
None yet
2 participants
@mgacc0

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

This comment has been minimized.

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 in 166db8a Oct 5, 2015

@mgacc0

This comment has been minimized.

mgacc0 commented Oct 6, 2015

Thanks

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