eqcut: Cut a continuous variable into equal-sized groups.

View source: R/eqcut.R

eqcutR Documentation

Cut a continuous variable into equal-sized groups.

Description

Cut a continuous variable into equal-sized groups.

Usage

eqcut(
  x,
  ngroups,
  labeling = eqcut.default.labeling,
  withhold = NULL,
  varlabel = if (has.label(x)) label(x) else deparse(substitute(x)),
  quantile.type = 7,
  right = FALSE,
  ...
)

eqcut.default.labeling(x, xcat, which, what, from, to, ...)

Arguments

x

A numeric vector.

ngroups

The number of groups desired.

labeling

A function that produces the category labels (see Details).

withhold

A named list of logical vectors (see Details).

varlabel

A character string to be used as a label for x, or NULL.

quantile.type

An integer from 1 to 9, passed as the type argument to function quantile.

right

Should intervals be right-closed? (passed to cut).

...

Further arguments passed on to function labeling.

xcat

A factor returned by cut.

which, what

Character vectors for labeling the categories in an appropriate way (see Examples).

from, to

Numeric vectors giving the ranges covered by the categories of x.

Details

The function labeling must have the signature function(x, xcat, which, what, from, to, ...) and produces the character vector of factor levels. See below for an example.

The withhold list can be used when x contains special values that should not be considered in the calculation of the quantiles used to create the ngroups categories. The special values are given a label that corresponds to the name of the corresponding list element. See below for an example.

Value

A factor of the same length as x. There are ngroups levels plus one additional level for each element of withhold.

Functions

  • eqcut.default.labeling(): The default labeling function.

See Also

cut quantile

Examples

x <- sample(100)
table(eqcut(x, 2))
table(eqcut(x, 3))
table(eqcut(x, 4))
table(eqcut(x, 5))
table(eqcut(x, 6))
table(eqcut(x, 7))
table(eqcut(x, 8))

# An example of using eqcut in a table with custom labeling function.
dat <- expand.grid(id=1:100, sex=c("Male", "Female"), treat=c("Treated", "Placebo"))
dat$age <- runif(nrow(dat), 18, 50)
dat$wt <- exp(rnorm(nrow(dat), log(75 + 10*(dat$sex=="Male")), 0.2))
dat$auc <- ifelse(dat$treat=="Placebo", NA, exp(rnorm(nrow(dat), log(1000), 0.34)))
dat$auc[3] <- NA  # Add a missing value

label(dat$sex) <- "Sex"
label(dat$age) <- "Age"
label(dat$wt)  <- "Weight"
label(dat$auc) <- "AUC"
units(dat$age) <- "y"
units(dat$wt)  <- "kg"
units(dat$auc) <- "ng.h/mL"

w <- list(Placebo=(dat$treat=="Placebo"), Excluded=is.na(dat$auc))
f <- function(x, xcat, which, what, from, to, ...) {
   what <- sub("of ", "of<br/>", what)
   sprintf("%s %s<br/>&ge;%s to &lt;%s",
       which, what, signif_pad(from, 3, FALSE), signif_pad(to, 3, FALSE))
}
table1(~ sex + age + wt | eqcut(auc, 3, f, w), data=dat)


table1 documentation built on Jan. 6, 2023, 5:07 p.m.