From 0791b6db3466337ce3bef91eebb14852142aee68 Mon Sep 17 00:00:00 2001 From: Achim Zeileis Date: Tue, 3 Sep 2024 18:47:38 +0200 Subject: [PATCH] added as.character() and duplicated() methods (#106) --- NAMESPACE | 2 ++ NEWS.md | 8 ++++++++ R/utils.R | 13 +++++++++++++ 3 files changed, 23 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 656b605..0c784d6 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ S3method("[",distribution) S3method("names<-",distribution) +S3method(as.character,distribution) S3method(as.data.frame,distribution) S3method(as.list,distribution) S3method(as.matrix,distribution) @@ -41,6 +42,7 @@ S3method(cdf,ZTNegativeBinomial) S3method(cdf,ZTPoisson) S3method(dim,distribution) S3method(dimnames,distribution) +S3method(duplicated,distribution) S3method(fit_mle,Bernoulli) S3method(fit_mle,Binomial) S3method(fit_mle,Exponential) diff --git a/NEWS.md b/NEWS.md index 18ad9e1..f60871d 100755 --- a/NEWS.md +++ b/NEWS.md @@ -20,10 +20,18 @@ simplified. For example, now `Normal(mu = 0, sigma = 1)` is used instead of `Normal distribution (mu = 0, sigma = 1)` in order to yield a more compact output, especially for vectors of distributions (#101). +- Added an `as.character()` method which essentially calls `format(..., digits = 15, drop0trailing = TRUE)`. + This mimics the behavior and precision of base R for real vectors. Note that this enables + using `match()` for distribution objects. +- Added a `duplicated()` method which relies on the corresponding method for the `data.frame` + of parameters in a distribution. - Fixed errors in notation of cumulative distribution function in the documentation of `HurdlePoisson()` and `HurdleNegativeBinomial()` (by @dkwhu in #94 and #96). - The `prodist()` method for `glm` objects can now also handle `family` specifications from `MASS::negative.binomial(theta)` with fixed `theta` (reported by Christian Kleiber). +- Replace `ellipsis` dependency by `rlang` as the former will be + [deprecated/archived](https://rlang.r-lib.org/news/index.html#argument-intake-1-0-0) + (by @olivroy in #105). - Further small improvements in methods and manual pages. diff --git a/R/utils.R b/R/utils.R index 86c1e2a..a7d0bc0 100755 --- a/R/utils.R +++ b/R/utils.R @@ -289,6 +289,19 @@ format.distribution <- function(x, digits = pmax(3L, getOption("digits") - 3L), setNames(f, n) } +#' @export +as.character.distribution <- function(x, digits = 15L, drop0trailing = TRUE, ...) { + y <- format(x, digits = digits, drop0trailing = drop0trailing, ...) + if (!is.null(names(y))) names(y) <- NULL + return(y) +} + +#' @export +duplicated.distribution <- function(x, incomparables = FALSE, ...) { + class(x) <- "data.frame" + duplicated(x, incomparables = incomparables, ...) +} + #' @export print.distribution <- function(x, digits = pmax(3L, getOption("digits") - 3L), ...) { if (length(x) < 1L) {