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

Description Usage Arguments Details Value Functions See Also Examples

View source: R/eqcut.R

Description

Cut a continuous variable into equal-sized groups.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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

See Also

cut quantile

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 June 7, 2021, 1:07 a.m.