Title: | Interval and Enum-Type Representation of Vectors |
---|---|
Description: | Enum-type representation of vectors and representation of intervals, including a method of coercing variables in data frames. |
Authors: | Torsten Hothorn [aut, cre] |
Maintainer: | Torsten Hothorn <[email protected]> |
License: | GPL-2 |
Version: | 1.0-5 |
Built: | 2024-11-22 02:49:39 UTC |
Source: | https://github.com/cran/inum |
Elements of a vector are stored as a set of levels and an integer representing the enumeration.
enum(x)
enum(x)
x |
A vector. Currently, methods for factors, logicals, integers, and numeric vectors are implemented. |
The unique elements of x
are stored as a levels
attribute
to an integer representing the enumeration. levels
and nlevels
methods are available. This is essentially the same as factor
where
the levels can be arbitrary vectors, not just characters.
An object of class enum
. A value of 0
encodes NA
.
(ex <- enum(x <- gl(2, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(c(TRUE, FALSE), 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(1:5, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(1:5 + .5, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- c(NA, rep(1:5 + .5, 2)))) all.equal(c(NA, levels(ex))[unclass(ex) + 1L], x)
(ex <- enum(x <- gl(2, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(c(TRUE, FALSE), 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(1:5, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- rep(1:5 + .5, 2))) all.equal(levels(ex)[ex], x) (ex <- enum(x <- c(NA, rep(1:5 + .5, 2)))) all.equal(c(NA, levels(ex))[unclass(ex) + 1L], x)
interval
divides x
into intervals and,
unlike cut
, represents these as a numeric vector.
interval(x, ...) ## S3 method for class 'numeric' interval(x, breaks = 50, ...)
interval(x, ...) ## S3 method for class 'numeric' interval(x, breaks = 50, ...)
x |
A numeric vector. |
breaks |
Either a numeric vector of two or more unique cut points or a
single number (greater than or equal to 2) giving the number
of intervals into which |
... |
Additional arguments, currently ignored. |
This is just a wrapper around cut
where the resulting intervals
are stored as numeric values for simplified computation.
An object of class interval
. A value of 0
encodes NA
.
(ix <- interval(x <- 0:100/100, breaks = 0:10/10)) (cx <- cut(x, breaks = 0:10/10)) attr(ix, "levels") levels(ix) levels(cx) diag(table(ix, cx)) (ix <- interval(x <- c(NA, 0:100/100), breaks = 0:10/10)) ix[is.na(x)] unclass(ix)[is.na(x)]
(ix <- interval(x <- 0:100/100, breaks = 0:10/10)) (cx <- cut(x, breaks = 0:10/10)) attr(ix, "levels") levels(ix) levels(cx) diag(table(ix, cx)) (ix <- interval(x <- c(NA, 0:100/100), breaks = 0:10/10)) ix[is.na(x)] unclass(ix)[is.na(x)]
enum
or interval
Represents elements of a data frame as enum
or interval
.
inum(object, nmax = 20, ...) ## S3 method for class 'data.frame' inum(object, nmax = 20, ignore = NULL, total = FALSE, weights = NULL, as.interval = "", complete.cases.only = FALSE, meanlevels = FALSE, ...)
inum(object, nmax = 20, ...) ## S3 method for class 'data.frame' inum(object, nmax = 20, ignore = NULL, total = FALSE, weights = NULL, as.interval = "", complete.cases.only = FALSE, meanlevels = FALSE, ...)
object |
A data frame. |
nmax |
Maximal number of categories for each of the numeric variables. |
ignore |
A character vector of variable names not to be discretised. |
total |
A logical. |
weights |
An optional vector of weights. |
as.interval |
A character vector of variable names to be converted to |
complete.cases.only |
A logical. |
meanlevels |
A logical. |
... |
Additional arguments, currently ignored. |
Each variable in object
is converted to enum
or interval
.
An object of class inum
, basically a list of
enum
or interval
objects. If
total = TRUE
, an integer vector with a data frame
as levels
attribute is returned. In this case, 0
means NA
.
data("iris", package = "datasets") iris[1,1] <- NA inum(iris, nmax = 5) inum(iris, nmax = 5, total = TRUE) inum(iris, nmax = 5, total = TRUE, as.interval = "Sepal.Width", complete.cases.only = TRUE)
data("iris", package = "datasets") iris[1,1] <- NA inum(iris, nmax = 5) inum(iris, nmax = 5, total = TRUE) inum(iris, nmax = 5, total = TRUE, as.interval = "Sepal.Width", complete.cases.only = TRUE)