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

Result of sum() incorrect for Period objects #937

Closed
petrbouchal opened this issue Nov 28, 2020 · 3 comments
Closed

Result of sum() incorrect for Period objects #937

petrbouchal opened this issue Nov 28, 2020 · 3 comments

Comments

@petrbouchal
Copy link

petrbouchal commented Nov 28, 2020

When calling sum() on a Period object, the result returned is incorrect (and in any case different from the sum() of a Duration or Interval object.)

library(tibble)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union

df <- tibble(
  start = rep(now(), 3),
  added = c(10, 1000, 10000),
  end = start + dseconds(added),
  t_period = as.period(end - start),
  t_duration = as.duration(end - start),
  t_interval = interval(start, end)
)


sum(df$t_period)
#> [1] 90
sum(df$t_duration)
#> [1] 11010
sum(df$t_interval)
#> [1] 11010

df$t_period
#> [1] "10S"        "16M 40S"    "2H 46M 40S"

sum(df$t_period[1])
#> [1] 10
sum(df$t_period[2])
#> [1] 40
sum(df$t_period[3])
#> [1] 40

Created on 2020-11-28 by the reprex package (v0.3.0)

The sum() method seems to be taking in the .Data slot in the object, which only contains the seconds element of the period.

@petrbouchal petrbouchal changed the title Result of sum() differs between Period and Duration objects - only seconds summed for Periods Result of sum() incorrect for Period objects Nov 28, 2020
@vspinu
Copy link
Member

vspinu commented Dec 4, 2020

sum is not a generic unfortunately. I am not sure we want to mess with that. I am opening a discussion on generics package tracker. Let's see what other people think about this.

@vspinu
Copy link
Member

vspinu commented Nov 5, 2022

Closing in favor of the more general #918 . Will implement this soonish.

@vspinu vspinu closed this as completed Nov 5, 2022
@vspinu
Copy link
Member

vspinu commented Nov 5, 2022

Actually a true duplicate is #902

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