Skip to contents

A distribution object is one distribution, so length() gives 1 and is.na() gives a single logical. as.list() wraps the distribution in a list of one.

Usage

# S3 method for class 'dst'
length(x)

# S3 method for class 'dst'
is.na(x)

# S3 method for class 'dst'
as.list(x, ...)

Arguments

x

A distribution object.

...

Not used.

Value

For length(), the number 1. For is.na(), a single logical. For as.list(), a list containing the one distribution.

Details

A distribution is built out of a list of its properties — a CDF, a density, a mean — and without these methods base R reports on that list rather than on the distribution. length() counted the properties and is.na() tested each one, so dst_norm(0, 1) answered with eleven FALSEs. Neither answer was about the distribution.

is.na() is TRUE for the Null distribution (dst_null()) and FALSE for every other. The Null distribution is the missing value of the distribution world, so it is the one that is.na() finds.

Note that the properties are still reachable, and are still what the object is made of: x[["cdf"]] and names(x) are unchanged, and eval_property() is the supported way to get at them. Only the questions asked of the distribution as a whole now answer about the whole.

To hold several distributions, put them in a list; in a data frame, that is a list-column. A distribution does not have length beyond one.

Examples

d <- dst_norm(0, 1)
length(d)
#> [1] 1
is.na(d)
#> [1] FALSE

# The Null distribution is the missing one.
is.na(dst_null())
#> [1] TRUE

# Several distributions go in a list.
ds <- list(dst_norm(0, 1), dst_null(), dst_pois(3))
vapply(ds, is.na, logical(1))
#> [1] FALSE  TRUE FALSE