Skip to content

Commit

Permalink
added as.character() and duplicated() methods (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeileis authored Sep 3, 2024
1 parent 92fa9de commit 0791b6d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0791b6d

Please sign in to comment.