diff --git a/NAMESPACE b/NAMESPACE index a9884e9..5c3274b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/percent.R b/R/percent.R index 0b093c2..f704347 100644 --- a/R/percent.R +++ b/R/percent.R @@ -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, @@ -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), ...)) +}