Skip to content

Commit

Permalink
Added summary methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic-Chr committed Aug 27, 2024
1 parent 6bd2798 commit 9142f86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

S3method("[",percent)
S3method(Math,percent)
S3method(Ops,percent)
S3method(Summary,percent)
S3method(as.character,percent)
S3method(format,percent)
S3method(mean,percent)
S3method(print,percent)
S3method(rep,percent)
S3method(unique,percent)
Expand Down
35 changes: 19 additions & 16 deletions R/percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,6 @@ rep.percent <- function(x, ...){
out
}

#' @export
Ops.percent <- function(e1, e2){
math <- switch(.Generic,
`+` =,
`-` =,
`*` =,
`/` =,
`^` =,
`%%` =,
`%/%` = TRUE, FALSE)
if (inherits(e2, "percent") && !inherits(e1, "percent")){
e1 <- unclass(e1)
e2 <- unclass(e2)
}
NextMethod(.Generic)
}
#' @export
Math.percent <- function(x, ...){
rounding_math <- switch(.Generic,
Expand All @@ -192,3 +176,22 @@ Math.percent <- function(x, ...){
new_percent(out)
}
}
#' @export
Summary.percent <- function(x, ...){
summary_math <- switch(.Generic,
`sum` =,
`prod` =,
`min` =,
`max` =,
`range` = TRUE, FALSE)
x <- unclass(x)
out <- NextMethod(.Generic)
if (summary_math){
out <- new_percent(out)
}
out
}
#' @export
mean.percent <- function(x, ...){
new_percent(mean(unclass(x), ...))
}

0 comments on commit 9142f86

Please sign in to comment.